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

Inconsistent return value of valueOf() on differnt NodeJS versions #5207

Open
StefanPavlik opened this issue Aug 23, 2019 · 2 comments
Open

Comments

@StefanPavlik
Copy link

StefanPavlik commented Aug 23, 2019

Bug description
The return value of Moment.valueOf() is not consistent with different versions of NodeJS.
It seems that the problem started since NodeJS version 10.4.0

To reproduce
Run following code with different NodeJS verions:

for (let i = 1800; i < 2000; i++) {
  const dateStr = i + '-01-01';
  console.log(moment(dateStr).valueOf() + '    ' + dateStr);
}

Expected behavior
I think that return value of Moment.valueOf() should be the same regardless of NodeJS version.

Results from my computer:
moment.valueOf_on_node_8.16.1.txt
moment.valueOf_on_node_10.3.0.txt
moment.valueOf_on_node_10.4.0.txt
moment.valueOf_on_node_10.16.3.txt

Environment:

  • OS: Windows 10 1809 (64bit)
  • NodeJS (versions 8.16.1; 10.3.0; 10.4.0; 10.16.3)

Moment-specific environment:

console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(moment.version)

With NodeJS 8.16.1 the output is:

Fri Aug 23 2019 10:05:12 GMT+0200 (Central Europe Daylight Time)
2019-8-23 10:05:12
-120
2.24.0

With NodeJS 10.16.3 the output is:

Fri Aug 23 2019 10:05:26 GMT+0200 (Central European Summer Time)
8/23/2019, 10:05:26 AM
-120
2.24.0
@ashsearle
Copy link
Contributor

Confirmed.

One reason for the difference across Node versions is their every-increasing support for historic timezone data.

Browser/NodeJS parses ISO dates as though they represent midnight at the start of the day UTC.

new Date('1800-01-01T00:00:00.000Z').toISOString() // 1800-01-01T00:00:00.000Z 
new Date('1800-01-01').toISOString(); // 1800-01-01T00:00:00.000Z

moment's custom ISO parsing behaves differently:

moment('1800-01-01T00:00:00.000Z').toISOString(); // 1800-01-01T00:00:00.000Z
moment('1800-01-01').toISOString(); // 1800-01-01T00:01:15.000Z !! (Chrome 76)

I think moment originally implemented its own ISO parsing to support IE, but guess this is an unwanted consequence, and could be avoided if moment feature-detected and used built-in ISO-parsing support where available.

@StefanPavlik
Copy link
Author

To achieve the desired behavior one can use the moment.uct() constructor:

for (let i = 1800; i < 2000; i++) {
  const dateStr = i + '-01-01';
  console.log(moment.utc(dateStr).valueOf() + '    ' + dateStr);
}

Using the moment.utc() constructor the results of .valueOf() are the same across different Node.js versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants