Reputation: 8370
When I run this code, the first date is shown in GMT, and the second in BST. Why is this? The calls to Date.UTC
are identical apart from one changed digit, the month digit. That, to me, shouldn't warrant a change in timezone. Please note that I'm in London right now, so somehow the second date seems to be returning local time. Why is the timezone different for the two different dates?
var date1 = new Date(Date.UTC(2005,0,5,4,55,55));
alert(date1); // Wed Jan 05 2005 04:55:55 GMT+0000 (GMT)
var date2 = new Date(Date.UTC(2005,5,5,4,55,55)); // <-- 0 has been replaced by 5
alert(date2); // Sen Jun 05 2005 05:55:55 GMT+0100 (BST)
Upvotes: 0
Views: 50
Reputation: 24221
Using Date.UTC(), only effects setting the date using UTC.
As default Javascript dates display using localtime.
So if you wish to see the dates in UTC format,. You can't just use the default toString() implementation, as that will use localtime version.
But what you can do is use the UTC variants for display. eg. toUTCString()
and also toISOString()
.
var date2 = new Date(Date.UTC(2005,5,5,4,55,55));
//if you say live in the UK, this date in localtime is
//British Summer Time,..
//eg. Sun Jun 05 2005 05:55:55 GMT+0100 (GMT Summer Time)
//if your running this script from another country
//your likely to see something different.
console.log(date2.toString());
//here we show the date in UTC, it will be same
//whatever country your running this from
//eg. Sun, 05 Jun 2005 04:55:55 GMT
console.log(date2.toUTCString());
//for an easier to parse date,
//eg. 2005-06-05T04:55:55.000Z
console.log(date2.toISOString());
Upvotes: 1