0

When I convert that date to ISO string I get the 30th of november 2015. Why wouldn't it be first of december? I have googled and I know that month is 0-indexed and that overflows lead to the next day/month/year. But I cannot explain myself that behaviour and when I google it I find unrelated topics.

Rob
  • 14,746
  • 28
  • 47
  • 65
user3182532
  • 1,097
  • 5
  • 22
  • 37
  • 5
    Without seeing the code, best guess is that you live east of Greenwich and the ISO string is in UTC which is some hours behind your local time. Or the other way around. – JJJ Nov 14 '17 at 14:37
  • [Cant relate](http://jsbin.com/fapiduruna/edit?console) – Jonas Wilms Nov 14 '17 at 14:37
  • 1
    Please post the code you tried so we can evaluate it. Also review this post for pointers: https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – SteveB Nov 14 '17 at 14:38

3 Answers3

1

Thing is if You type:

var date = new Date(2015, 11, 1);
console.log(date);

You will get output based on your timezone, for me it's:

Tue Dec 01 2015 00:00:00 GMT+0100 (Central Europe Standard Time)

Function toISOString will always output time in UTC. So in this case you will get this date minus one hour.

2015-11-30T23:00:00.000Z
Anwarus
  • 138
  • 2
  • 11
0

If you check the MDN page you'll see that:

Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.

Your users have different local timezones. For me new Date(2015, 11, 1) gives Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time) (I'm in the timezone Central European Time which is GMT+1).

Hence you can follow the MDN hint and use Date.UTC inside of your date call instead:

var date = new Date(2015, 11, 1);

console.log(date.toString());
// "Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time)"
//                  00:00:00 in GMT+1 but 23:00:00 in GMT+0

console.log(date.toISOString());
// "2015-11-30T23:00:00.000Z"
//          ^^ 30th of november - that's a nogo!

var utcDate = new Date(Date.UTC(2015, 11, 1));

console.log(utcDate.toString());
// "Tue Dec 01 2015 01:00:00 GMT+0100 (Romance Standard Time) 15:49:26.146"
//                  01:00:00 in GMT+1 but 00:00:00 in GMT+0

console.log(utcDate.toISOString());
// "2015-12-01T00:00:00.000Z"
//          ^^ The first! Not the 30th!
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
-1
var date = new Date(Date.UTC(2015, 11, 1));
console.log(date.toISOString());

Output:

2015-12-01T02:00:00.000Z
ceferrari
  • 1,597
  • 1
  • 20
  • 25
  • 2
    Actually no, the output is `2015-11-30T23:00:00.000Z` depending on your setup. You're not considering different user timezones. – h2ooooooo Nov 14 '17 at 14:39
  • Well, according to [Mozilla Docs](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString), " The timezone is always zero UTC offset" – ceferrari Nov 14 '17 at 14:41
  • Yes but the _passed_ value is local time if you look at the MDN article for Date. – h2ooooooo Nov 14 '17 at 14:44
  • Uhm .. what? I just want to set a date in javascript, is it for real that I have to worry about timezones? EDIT: my timezone is Berlin / Germany – user3182532 Nov 14 '17 at 14:45
  • Updated my answer. You can create UTC date instead of local. – ceferrari Nov 14 '17 at 14:48
  • I'm really surprised that it doesn't behave as you would expect. I think Date(x,y,z) should just return the date that you've specified ... what's the reasoning behind this? Why wouldn't you just return the date that the user has requested? – user3182532 Nov 14 '17 at 14:50
  • Because the "toISOString()" treat every date as UTC – ceferrari Nov 14 '17 at 14:52
  • 1
    @user3182532 If you don't want to worry about timezones, operate in local time. Don't use the ISO time which is UTC by definition. The reason it takes timezones into account is, well, because they exist. Midnight in Berlin is 11 PM in London. – JJJ Nov 14 '17 at 14:52
  • ohhhh gosh of course, sorry. Now I've clicked. Of course it makes sense that I get this output if I convert to _ISO_ ! Using .toString() now instead ;) – user3182532 Nov 14 '17 at 14:57