Rolando
Rolando

Reputation: 62634

How to convert javascript date to GMT milliseconds?

I have the following date:

var datestr = "11/11/2012 10:55"

When I do the following:

var datems = new Date(datestr).getTime();

The milliseconds I get do not appear to be the correct milliseconds as it appears to be much farther ahead in time. How do i convert the "datestr" above to milliseconds (in respect to GMT)?

Upvotes: 3

Views: 829

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

One possibility is that Date assumes local time if the string doesn't specify a timezone.

If all of your date strings are in that format, you can append a timezone to them when parsing:

var datems = new Date(datestr + " UTC").getTime();

Or you'll have to use the local offset to find UTC:

var localDate = new Date(datestr);
var datems = localDate.getTime() - (localDate.getTimezoneOffset() * 60 * 1000);

Upvotes: 3

Related Questions