Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First day and last day in a month not behaving like in the browser. (Date object) #9333

Closed
DimiTech opened this issue Oct 28, 2016 · 4 comments
Labels
question Issues that look for answers.

Comments

@DimiTech
Copy link

  • Version: v6.3.0
  • Platform: Windows 7 (64bit)
  • Subsystem: Node Core Runtime - Date object.

This is the code I use to get the first day and the last day of the month in the browser:

var firstDay = new Date((new Date()).getFullYear(), (new Date()).getMonth(), 1);
var lastDay  = new Date((new Date()).getFullYear(), (new Date()).getMonth() + 1, 0);
console.log(firstDay);
console.log(lastDay);

The browser gives the following (correct) results:
Sat Oct 01 2016 00:00:00 GMT+0200 (Central Europe Daylight Time)
Mon Oct 31 2016 00:00:00 GMT+0100 (Central Europe Standard Time)

Yet the Node runtime gives these results:
2016-09-30T22:00:00.000Z
2016-10-30T23:00:00.000Z

Disregarding the formatting, the numerical values for month and day aren't what they are when the same code is executed in the browser.

Has anyone else noticed this?
Thanks.

@mscdex
Copy link
Contributor

mscdex commented Oct 28, 2016

It looks like it's possibly a difference in timezones (between node and the browser)? I get the same result both in the browser and in node v6.9.1 on the same machine.

What do you see if you log firstDay.toString() and lastDay.toString() from node?

@mscdex mscdex added the question Issues that look for answers. label Oct 28, 2016
@DimiTech
Copy link
Author

DimiTech commented Oct 28, 2016

Interesting. I assume that this is a simple off by one bug but I'm not familiar with the codebase enough in order to mess with it at this point.

@mscdex

var firstDay = new Date((new Date()).getFullYear(), (new Date()).getMonth(), 1);
var lastDay  = new Date((new Date()).getFullYear(), (new Date()).getMonth() + 1, 0);
console.log(firstDay.toString());
console.log(lastDay.toString());

gives the wanted year-month-date:
Sat Oct 01 2016 00:00:00 GMT+0200 (Central Europe Daylight Time)
Mon Oct 31 2016 00:00:00 GMT+0100 (Central Europe Standard Time)

Same result (regarding the year-month-date values) can be obtained by:

var firstDay = new Date((new Date()).getFullYear(), (new Date()).getMonth(), 2);
var lastDay = new Date((new Date()).getFullYear(), (new Date()).getMonth() + 1, 1);
console.log(firstDay);
console.log(lastDay);

2016-10-01T22:00:00.000Z
2016-10-31T23:00:00.000Z

Thanks :)

@mscdex
Copy link
Contributor

mscdex commented Oct 28, 2016

Ok, so node agrees with the browser then. There is no discrepancy here. It's a matter of timezones.

The long form (Sat Oct 01 2016 00:00:00 GMT+0200 (Central Europe Daylight Time)) is in local time, but when Date objects are inspected in node (e.g. with date.toISOString()), the UTC time is used.

The UTC dates make sense because the hour is not passed to the constructor, so the default of midnight is used. Since the timezone (for firstDay) at the start of October is GMT-2, you see a date of 9/30/2016 22:00 because you need to subtract 2 hours from midnight 10/1/2016 to get the UTC date. This is why you see the value for firstDay. It's a similar deal with lastDay, except due to the change in daylight savings time, only one hour is subtracted (giving you 23:00 on the previous day).

@DimiTech
Copy link
Author

DimiTech commented Oct 28, 2016

@mscdex Appreciate the super clear explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants