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.
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
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)
mm/dd/yyyyT00:00:00.000Z
is not the correct format. It should be yyyy-mm-ddT00:00:00.000Z
, and this does work. –
Saprogenic 2024-04-26T00:00:00Z
actually creates and displays the date as april 25. (Chrome and FF). Currently the only way I can get the date I want is splitting on "-" and assembling a date from the parts. –
Finbur 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);
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).
Here is what I would do.
var current = new Date();
var utcDate = new Date(current.getTime() + current.getTimezoneOffset() * 60000);
© 2022 - 2024 — McMap. All rights reserved.
new Date('2015-08-27').toISOString()
– Southwestwards