-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Comments
You need to specify a format string. moment('1999-12-4', 'Y-M-D') |
To be clear - |
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 If Moment can't parse the string correctly, it should pass the unaltered string to |
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:
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. |
I'm working on a fix now. |
…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.
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. |
[bugfix] Fix #3500: ISO 8601 parsing should match the full string, not the beginning of the string.
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 tonew 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:
Please include the values of all variables used.
Environment:
Moment 2.15.1, Node 4.4.4, Ubuntu 16.04
The text was updated successfully, but these errors were encountered: