I need to generate a javascript Date
from a UTC date string with the format YYYY-MM-DD
. Here is an example:
var date = new Date('2018-01-31');
The date this creates is:
Tue Jan 30 2018 19:00:00 GMT-0500 (Eastern Standard Time)
Given the time zone offset, the date is yesterday.
How can I create a javascript Date that assumes the timeless input is UTC?
I need to generate a javascript Date
from a UTC date string with the format YYYY-MM-DD
. Here is an example:
var date = new Date('2018-01-31');
The date this creates is:
Tue Jan 30 2018 19:00:00 GMT-0500 (Eastern Standard Time)
Given the time zone offset, the date is yesterday.
How can I create a javascript Date that assumes the timeless input is UTC?
Share Improve this question asked Jan 31, 2018 at 21:16 mellis481mellis481 4,16712 gold badges77 silver badges130 bronze badges 2- You can use momentjs. and momentjs./timezone is easy to use – Jorge Mejia Commented Jan 31, 2018 at 21:21
-
Unless you manipulate the prototype (not suggested), you can't force the Date object into the timezone you want without an if statement. You could do something like
if(!timezone) date = Date.UTC('2018-01-31');
and thennew Date
, but I believe .UTC doesn't accept strings – Sterling Archer Commented Jan 31, 2018 at 21:23
3 Answers
Reset to default 4You can use Date.UTC .
This is an example from https://developer.mozilla/de/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
var utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
var utcDate2 = new Date(Date.UTC(0, 0, 0, 0, 0, 0));
console.log(utcDate1.toUTCString());
// expected output: Fri, 02 Feb 1996 03:04:05 GMT
console.log(utcDate2.toUTCString());
// expected output: Sun, 31 Dec 1899 00:00:00 GMT
So you get this as solution:
var test = "2018-01-31";
var year = test.substr(0,4);
var month = test.substr(5,2) -1;
var day = test.substr(8,2);
var utcDate1 = new Date(Date.UTC(year,month,day));
alert(utcDate1.toUTCString());
// expected output:Wed, 31 Jan 2018 00:00:00 GMT
You can use a bination of Date.UTC
, split
and the spread
(operator syntax...
is not an operator):
var convertedDate = '2018-01-31'.split('-');
convertedDate[1] = convertedDate[1] - 1 // Date.UTC expects a value from 0-11
var date = new Date(Date.UTC(...convertedDate))
console.log(date.toDateString());
It is important to note that Date.UTC
expects a month form 0-11 (for some reason)
I call toISOString()
, split it on the 'T' delimiter, and take the first substring:
var date = new Date('2018-01-31')
var utcDate = date.toISOString().split('T')[0];
or in a one liner:
var utcDate = (new Date('2018-01-31')).toISOString().split('T')[0];
I also wrote this reusable function:
/**
* returns the UTC date value in the format yyyy-mm-dd, e.g. converts a JS date
* of "Wed Oct 16 2024 10:57:39 GMT-0700 (Pacific Daylight Time)" to "2024-10-16",
* or an empty string if it is not a valid date
*
* @datetime a string, integer, or Date object, defaults to now
*/
function getUTCDate(datetime=new Date()) {
var date = new Date(datetime);
if (isNaN(date.getYear()))
return "";
return date.toISOString().split("T")[0];
}