简体   繁体   中英

How to convert normal date to javascript date

I have a date like 2012-08-09T20:37:52.041 i want to subtract this time from the current time. To use the getTime() function and subtract the time i need this time in a parsable format. How do I do that? Or is there any other way to subtract a time like 2012-08-09T20:37:52.041 from the current time?

Use a date parsing library like http://www.datejs.com/ . With that, you can do this:

Date.parse('2012-08-09T20:37:52') // You'll have to remove the milliseconds

It's really super hard to parse dates in JavaScript.

var datestring = "2012-08-09T20:37:52.041";
var date = new Date(datestring);
var diff = (new Date()).getTime() - date.getTime(); // in milliseconds

I'm joking, it's super easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM