Convert the format yyyy-MM-ddTHH:mm:ss to datetime in javascript which is acceptable by getTime() function

Abhay picture Abhay · Nov 9, 2016 · Viewed 8.2k times · Source

I have a date in the format yyyy-MM-ddTHH:mm:ss and I want to convert it to datetime format in such a way that if the result is evaluated via getTime() method, it will return number of milliseconds. For now, the method I have tried is throwing the error : Uncaught TypeError: dt.getTime is not a function where dt is the resultant date. Please state a way to achieve this using javascript.

Thanks in advance.

Answer

vlatkokaplan picture vlatkokaplan · Nov 9, 2016

If your date is string, you should initialize new Date object with your time string as parameter. getTime() is method of Date object. Note that it may not work in all browsers.

var date = new Date('2016-11-09T18:00:01');
alert(date.getTime())

https://jsfiddle.net/rxo5zz71/