admin管理员组

文章数量:1336632

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.

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.

Share Improve this question asked Nov 9, 2016 at 11:36 AbhayAbhay 351 gold badge1 silver badge4 bronze badges 3
  • I have a date in the format yyyy-MM-ddTHH:mm:ss. Is it a date or string ? – Pugazh Commented Nov 9, 2016 at 11:41
  • would you consider using MomentJS or not? – Marek Urbanowicz Commented Nov 9, 2016 at 11:42
  • @Pugazh It is in string format @ Marek Urbanowicz If there is an option to achieve this without using MomentJS it would be better. Otherwise plz suggest the MomentJS way. – Abhay Commented Nov 9, 2016 at 11:45
Add a ment  | 

1 Answer 1

Reset to default 4

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/rxo5zz71/

本文标签: