Artem Kalinchuk
Artem Kalinchuk

Reputation: 6642

JavaScript - Parse UTC Date

How can I parse a simple date string and let JavaScript know that it's actually a UTC date? Currently, if I do new Date('2015-08-27') it converts it to my timezone.

Upvotes: 54

Views: 57947

Answers (5)

T.Todua
T.Todua

Reputation: 56341

In some cases, when other solutions don't work, adding GMT will help:

new Date('July 11, 2022, 16:22:14 PM' + ' GMT')

(note, an user below in comment, reports bug about this)

Upvotes: 6

Holtwick
Holtwick

Reputation: 1967

These two helper functions move UTC date to local timezone and vice-versa

For your problem you would call: datetimeToUTC(new Date('2015-08-27')).


/** If you parsed a date without time zone, you may need to shift it to local time */
function datetimeToLocal(fromDate) {
  return new Date(
    fromDate.getUTCFullYear(),
    fromDate.getUTCMonth(),
    fromDate.getUTCDate(),
    fromDate.getUTCHours(),
    fromDate.getUTCMinutes(),
    fromDate.getUTCSeconds(),
    fromDate.getUTCMilliseconds(),
  )
}

/** If you parsed a date without time zone, you may need to shift it to UTC time */
function datetimeToUTC(fromDate) {
  return new Date(Date.UTC(
    fromDate.getFullYear(),
    fromDate.getMonth(),
    fromDate.getDate(),
    fromDate.getHours(),
    fromDate.getMinutes(),
    fromDate.getSeconds(),
    fromDate.getMilliseconds(),
  ))
}

The implementation is part of the zeed library (MIT).

Upvotes: 0

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

This might be obvious to most, but I got stumped for a few seconds because my string already had hh:mm:ss, so it required a little bit of string manipulation.

var d = '2022-09-14 13:20:31';
d = d.split(' ').join('T')+'Z';
var date = new Date(d);
console.log(date);

This version is more verbose, but feels sturdier to me.

var d = '2022-09-14 13:20:31';
var [yyyy, mm, dd, hh, m, s] = d.split(/[^\d]+/);
var date = new Date();
date.setUTCFullYear(+yyyy);
date.setUTCMonth(mm-1);
date.setUTCDate(+dd);
date.setUTCHours(+hh);
date.setUTCMinutes(+m);
date.setUTCSeconds(+s);
console.log(date);

Upvotes: 3

potatopeelings
potatopeelings

Reputation: 41075

You can do append 'T00:00:00.000Z' to make the time zone specific (Z indicates UTC)

new Date('2015-08-27' + 'T00:00:00.000Z')

Note that new Date('2015-08-27') is treated differently in ES5 (UTC) vs. ES6 (Local), so you can't expect it any correction to be work consistently if you were planning to to hard code it (i.e. don't do it)


Also, do note that your console.log might show you the local time corresponding to the UTC time the expression evaluates to (that tends to throw you off a bit if you are expecting UTC to be at the end for expression that evaluate to UTC times and your local time zone at the end for those that evaluate to your local time). For instance

new Date('2015-08-27T00:00:00.000Z')

could show

Thu Aug 27 2015 1:00:00 GMT+100

which is the same as

Thu Aug 27 2015 00:00:00 UTC

Upvotes: 49

Max Pringle
Max Pringle

Reputation: 37

Here is what I would do.

var current = new Date();
var utcDate = new Date(current.getTime() + current.getTimezoneOffset() * 60000);

Upvotes: -6

Related Questions