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

moment fails to parse '1999-12-4', when new Date() can parse it correctly. #3500

Closed
markstos opened this issue Oct 13, 2016 · 6 comments
Closed
Labels

Comments

@markstos
Copy link
Contributor

Description of the Issue and Steps to Reproduce:

moment parses '1999-12-4' as '1999-12-1'. From reading the Moment docs, this string is not in ISO 8601 format, so it would be passed to new Date(). However, manually passing the string to new Date() produces the correct parsing, as seen below.

I looked through other existing issues could not find that this case had already been reported.

Reproduction in the Node.js console:

> moment('1999-12-4').toString();
'Wed Dec 01 1999 00:00:00 GMT-0500'
> new Date('1999-12-4');
Sat Dec 04 1999 00:00:00 GMT-0500 (EST)

Please include the values of all variables used.

Environment:

Moment 2.15.1, Node 4.4.4, Ubuntu 16.04

Thu Oct 13 2016 13:31:06 GMT-0400 (EDT)
10/13/2016, 1:31:06 PM
240
2.15.1
@mattjohnsonpint
Copy link
Contributor

You need to specify a format string.

moment('1999-12-4', 'Y-M-D')

@mattjohnsonpint
Copy link
Contributor

To be clear - new Date(string) is only required to parse ISO8601 by the ECMAScript spec, and it does it poorly and inconsistently across different implementations. Beyond that, it can parse whatever it likes - which again leads to inconsistencies. We don't like falling back to the Date constructor, and are not looking for feature parity with any particular implementation.

@markstos
Copy link
Contributor Author

So you are OK with this silent parsing failure? If I need to specify a format string to get a correct result, why isn't it required?

It seems this string is detectably NOT an ISO 8601 string, since it doesn't follow the YYYY-MM-DD format.

Also, bug is clearly not with new Date() in my environment. As my sample demonstrated, new Date() gives the correct result for this string in my environment. It seems moment is somehow transforming the string before it's passed to new Date() and that's the bug.

If Moment can't parse the string correctly, it should pass the unaltered string to new Date()

@markstos
Copy link
Contributor Author

Also note that the example does not trigger the "Is not ISO" deprecation warning, indicating that date--which is not ISO format-- is being recognized and treated as a ISO date anyway, which is a bug.

From reading the related code here:

https://github.com/moment/moment/blob/develop/src/lib/create/from-string.js

It looks like there may be a problem with one the ISO regexes. As I read them, they anchor matches to the beginning of the input string and as long as they find a match at the beginning, the end of the string is ignored. Here's another example which shows that:

> moment('1999-12-this could be anything').toString();
'Wed Dec 01 1999 00:00:00 GMT-0500'

This could be fixed by making sure the ISO regex is designed to match the entire string, not just content at the beginning of the string. That would prevent this kind of garbage-in-garbage-out bug.

@markstos
Copy link
Contributor Author

I'm working on a fix now.

markstos added a commit to markstos/moment that referenced this issue Oct 13, 2016
…he beginning of the string.

Because of the nature of the how related tests are put together, the newly added test
*passes* even before the fix is applied.

This happens because the "non-ISO" tests are tested against the same ISO which is being fixed.

Because the broken regex is also used in the test, it allowed the non ISO date to be validated
before the fix.
@markstos
Copy link
Contributor Author

Please check out related PR #3502 for my proposed fix and discussion of other non-ISO dates in the test suite which the fix turned up.

@icambron icambron added the Bug label Oct 19, 2016
ichernev added a commit that referenced this issue Nov 6, 2016
[bugfix] Fix #3500: ISO 8601 parsing should match the full string, not the beginning of the string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants