Here are five cases. The results of the last three cases are surprising to me.
// Firefox 40.0.3
// Eastern time zone (ET)
var d;
d = new Date("January 1, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Thu, 01 Jan 2015 05:00:00 GMT
d = new Date("January 2, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 2 UTC: Fri, 02 Jan 2015 05:00:00 GMT
d = new Date("January 1, 2015 00:00:00 GMT");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 11 Date: 31 UTC: Thu, 01 Jan 2015 00:00:00 GMT
d = new Date("2015-01-01");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 11 Date: 31 UTC: Thu, 01 Jan 2015 00:00:00 GMT
d = new Date("2015-01-02");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Fri, 02 Jan 2015 00:00:00 GMT
It must be some discrepancy between UTC and local time. I see part of the answer. The function getDate() returns the specified date according to local time.
It seems that for getDate(), 00:00:00 GMT is the previous day in local time, so I'm getting the previous date instead of what I expected.
Maybe what I should really be asking is why does the Date constructor sometimes interpret the argument as local time, and sometimes as UTC time? What are rules on that?
jsfiddle
Here are five cases. The results of the last three cases are surprising to me.
// Firefox 40.0.3
// Eastern time zone (ET)
var d;
d = new Date("January 1, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Thu, 01 Jan 2015 05:00:00 GMT
d = new Date("January 2, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 2 UTC: Fri, 02 Jan 2015 05:00:00 GMT
d = new Date("January 1, 2015 00:00:00 GMT");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 11 Date: 31 UTC: Thu, 01 Jan 2015 00:00:00 GMT
d = new Date("2015-01-01");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 11 Date: 31 UTC: Thu, 01 Jan 2015 00:00:00 GMT
d = new Date("2015-01-02");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Fri, 02 Jan 2015 00:00:00 GMT
It must be some discrepancy between UTC and local time. I see part of the answer. The function getDate() returns the specified date according to local time.
It seems that for getDate(), 00:00:00 GMT is the previous day in local time, so I'm getting the previous date instead of what I expected.
Maybe what I should really be asking is why does the Date constructor sometimes interpret the argument as local time, and sometimes as UTC time? What are rules on that?
jsfiddle
Share Improve this question edited May 24, 2017 at 18:44 Dan Cron asked Sep 24, 2015 at 19:08 Dan CronDan Cron 1,1551 gold badge12 silver badges24 bronze badges 1- 1 hard to see when you only look at UTC string...look at the iso string...will show your timezone offset – charlietfl Commented Sep 24, 2015 at 19:19
3 Answers
Reset to default 6See the documentation
Specifically - "If a time zone is not specified and the string is in an ISO format recognized by ES5, UTC is assumed. GMT and UTC are considered equivalent. The local time zone is used to interpret arguments in RFC2822 Section 3.3 format (or any format not recognized as ISO 8601 in ES5) that do not contain time zone information"
and
"Given a date string of "March 7, 2014", parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC. Therefore Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC"
The last 2 of your examples are "ISO format recognized by ES5"
Date("January 1, 2015") is interpreted as local date.
Date("2015-01-01") is interpreted as a UTC date in ES5, local date in ES6.
getDate() gets the local date.
getUTCdate() gets the UTC date.
When you mix and match local and UTC, then you get surprising results.
Thanks, @preston-s. Your ments took me a long way towards answering my question. But, it's interesting that the interpretation of Date("2015-01-01") will change from UTC in ES5 to local time in ES6. Does anyone know when the different browsers are expected to switch over to the new behavior? I would expect some things to break at that point in time.
ES5: 'The value of an absent time zone offset is “Z”.'
ES6: 'If the time zone offset is absent, the date-time is interpreted as a local time.'
Given all this information, here are two solutions which will work across ES5 and ES6. Several other solutions are possible.
1) Use local date in and out.
var d;
d = new Date("January 1, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Thu, 01 Jan 2015 05:00:00 GMT
2) Use UTC date in and out. Spell out the time zone so this will still work in ES6.
var d;
d = new Date("2015-01-01T00:00:00.000Z");
alert('UTC Month: ' + d.getUTCMonth() + ' UTC Date: ' + d.getUTCDate() + ' UTC: ' + d.toUTCString());
// UTC Month: 0 UTC Date: 1 UTC: Thu, 01 Jan 2015 00:00:00 GMT
if u want getDate() function to return the date as 01 instead of 1, here is the code for it.... Lets assume Today's date is 01-11-2018
var today = new Date();
today = today.getFullYear()+ "-" + (today.getMonth() + 1) + "-" + today.getDate();
console.log(today); //Output: 2018-11-1
today = today.getFullYear()+ "-" + (today.getMonth() + 1) + "-" + ((today.getDate() < 10 ? '0' : '') + today.getDate());
console.log(today); //Output: 2018-11-01