Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert javascript date to GMT milliseconds?

Tags:

javascript

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)?

like image 473
Rolando Avatar asked Jan 24 '26 23:01

Rolando


1 Answers

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);
like image 126
Jonathan Lonowski Avatar answered Jan 26 '26 15:01

Jonathan Lonowski