-1

How to convert the following 1. 7. 2015 16:25 into date and time format using javascript?

In the below mentioned image how to convert the these two values in to datetime with the following pattern

Day.month.year Hour:minute:second;

lepe
  • 24,677
  • 9
  • 99
  • 108
  • Which image? What time zone? Have you tried anything yet that gives some results? Are you merely looking for code help? Try to read up a bit and see what you find http://stackoverflow.com/questions/7151543/convert-dd-mm-yyyy-string-to-date – Kalle Jul 01 '15 at 11:07
  • Do you receive "1. 7. 2015 16:25" as a text string and want to isolate day, month, year, hour and minute? – Niddro Jul 01 '15 at 11:07
  • Try to use moment.js http://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js – suvroc Jul 01 '15 at 11:08
  • Using `Date.parse()` might be your best bet. But I'd do some reading to be sure. – jonny Jul 01 '15 at 11:08
  • what is your image? what do you want to convert from "1. 7. 2015 16:25" into what?? basically what is your input and expected output? – Nomesh DeSilva Jul 01 '15 at 11:20
  • You parse out the components (month, day, year, hour etc) and construct a Date object using the long Date constructor. – Salman A Jul 01 '15 at 11:33

2 Answers2

0

Use moment JS https://momentjs.com/

function momentTest() {

  var varDate = "2018-01-19 18:05:01.423";
  var myDate =  moment(varDate,"YYYY-MM-DD").format("DD-MM-YYYY");
  var todayDate = moment().format("DD-MM-YYYY");  
  var yesterdayDate = moment().subtract(1, 'days').format("DD-MM-YYYY");   
  var tomorrowDate = moment().add(1, 'days').format("DD-MM-YYYY");

  alert(todayDate);

  if (myDate == todayDate) {
    alert("date is today");
  } else if (myDate == yesterdayDate) {
    alert("date is yesterday");
  } else if (myDate == tomorrowDate) {
    alert("date is tomorrow");
  } else {
    alert("It's not today, tomorrow or yesterday!");
  }
}
gareth
  • 199
  • 1
  • 3
-2
function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}

this works only if you're using moment.js.

Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43
ata
  • 11
  • 4
  • 1
    you must mention what you're using (moment.js) and how this code snippet will solve his problem with a proper description rather than posting any code samples. please read StackOverFlow help guide. – Nomesh DeSilva Jul 01 '15 at 11:24