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

Date.parse() returns null instead of NaN #263

Open
jpettitt opened this issue Mar 2, 2017 · 6 comments
Open

Date.parse() returns null instead of NaN #263

jpettitt opened this issue Mar 2, 2017 · 6 comments
Assignees
Milestone

Comments

@jpettitt
Copy link

jpettitt commented Mar 2, 2017

After installing datejs Date.parse() returns null instead of NaN

This breaks compatibility with the stock Date() object which in turn breaks the conditional request logic in express.static causing spurious HTTP 412 responses.

Gross hack work around

require('datejs');

module.exports = function fixdate() {
  Date._datejs_parse = Date.parse;
  Date.parse = function parse(str) {
    return this._datejs_parse(str) || NaN;
  };
};
@abritinthebay
Copy link
Owner

Ah, interesting quirk.
Good find.

@dougwilson
Copy link

The spec for Date.parse is https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.2

Unrecognisable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

@dougwilson
Copy link

The native Date.parse is also required to return a plain Number, but this overrides the global Date.parse to return an object instead.

@dougwilson
Copy link

@jpettitt if I'm following the issue #198 correctly, you cannot use datejs with Express until v2 of datejs.

@dougwilson
Copy link

FWIW, the biggest issue with the difference is that it's not even safe to perform basic inequality operations on the result of Date.parse like you can do natively.

For example, given Date.parse('foo') < Date.parse('2017-01-01T00:00:00Z'), without datejs loaded, that returns false, but when it's loaded, even though it still fails to parse the "foo" string, it now returns true.

@abritinthebay
Copy link
Owner

abritinthebay commented Mar 6, 2017

Indeed. We're never going to be 100% compatible with the Native parse (ie - being a number) because... well.. that's the point of this library (to extend the native Date object).

However this (returning NaN) seems like a good general compatibility change. Quite reasonable.

Would require a SemVer Major though, potentially, as it's a breaking backwards compatible change.

PRs welcome - as I'm snowed under with "real" work so OSS I'm afraid is low priority.

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