4

I've created a date in JS like so:

var myDate = new Date('2013-01-01 00:00:00');

I assume JS reads this in as UTC time. But when I do something like myDate.getTime() the timestamp returned was something like 4AM GMT time.

Why is this? And how do I get the date as midnight in UTC time?

Abbasov Alexander
  • 1,848
  • 1
  • 18
  • 27
Andy Hin
  • 30,345
  • 42
  • 99
  • 142
  • 1
    possible dupe: http://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc – stackErr Jul 11 '13 at 14:51
  • Nice resource: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate – DevlshOne Jul 11 '13 at 14:52
  • Which timezone are you based in? – Adrian Wragg Jul 11 '13 at 14:53
  • I think the issue is, JS is interpreting that date string as EST timezone, where I want it as UTC. I cannot use Date.UTC() because I have a date string, not the day, month, hour, etc. – Andy Hin Jul 11 '13 at 14:54
  • Does this answer your question? [How do you convert a JavaScript date to UTC?](https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – Michael Freidgeim Jan 09 '22 at 04:06

4 Answers4

2

At least in Chrome, this works:

var myDate = new Date('2013-01-01 00:00:00 UTC');

It also works if you put GMT instead of UTC. But I don't know if this is cross-browser enough.

2

I live in India. Hence my timezone is the Indian Standard Time (IST) which is listed in the tz database as Asia/Kolkata. India is 5 hours 30 minutes ahead of GMT. Hence when I execute new Date("2013-01-01 00:00:00") the actual time at GMT is "2012-12-31 18:30:00".

I believe you live in America because you're in the EST timezone (GMT-04:00)? Am I right?

If you want to parse the time at GMT instead of your local timezone then do this:

new Date("2013-01-01T00:00:00+00:00");

Notice the capital T between the date and the time, and the +00:00 at the end. This is the format used to parse a given time in a specific timezone.


Given the date string "2013-01-01 00:00:00" you can convert it to the required format using the following function:

function formatDateString(string, timezone) {
    return string.replace(" ", "T") + timezone;
}

Then you can create the date as follows:

new Date(formatDateString("2013-01-01 00:00:00", "+00:00"));

Another way to convert local time to GMT is as follows:

var timezone = new Date("1970-01-01 00:00:00"); // this is the start of unix time

Now that you have your own local timezone as a date object you can do:

new Date(new Date("2013-01-01 00:00:00") - timezone);

All the above methods produce the same date at GMT.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • All good ideas, except for the last one. That won't hold up if the offset at `1/1/1970` is different than the offset at the input date. Also, you could use a `Z` instead of `+00:00`, but both are valid. – Matt Johnson-Pint Jul 11 '13 at 16:16
0

JS reads this with time zone that your computer uses.

You can try use myDate.toUTCString() for get date in UTC time.

If you want get timestamp use myDate.getTime()

Abbasov Alexander
  • 1,848
  • 1
  • 18
  • 27
-1

Mine works simply by doing this

var datetime= new Date()

However the month is 1 low so you have to add one

Vega
  • 27,856
  • 27
  • 95
  • 103