Kira
Kira

Reputation: 965

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?

Upvotes: 0

Views: 216

Answers (2)

Halcyon
Halcyon

Reputation: 57719

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.

Upvotes: 1

tb11
tb11

Reputation: 3106

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

Upvotes: 2

Related Questions