-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
improves documentation for isDate() #480
Conversation
It does take a string! The library validates strings only, and your dates are being coerced to a string before validation: > new Date(2015, 1, 26).toString()
'Thu Feb 26 2015 00:00:00 GMT+0900 (JST)' The date validator is fairly lenient which may be the cause of confusion (#476). Can you provide some examples that you found confusing? |
@chriso I see that I didn't investigate enough. My expectation was that
While looking into it I found #476, which says to use
Is it supposed to be that |
As noted in #476, the > validator.isDate('2011-12-21')
true
> validator.isDate('2015-02-29')
false The fact that your |
@chriso Version is 4.5.1 > new Date().getTimezoneOffset()
480 One test fails:
The same input fails in the browser. Here's a screenshot of the inspector: |
Thanks. Would you mind applying the following diff and the re-running the tests? diff --git i/validator.js w/validator.js
index 900e718..cd3b777 100644
--- i/validator.js
+++ w/validator.js
@@ -522,7 +522,8 @@
} else {
timezone = iso8601Parts[21];
if (!timezone) {
- return null;
+ // if no hour/minute was provided, the date is GMT
+ return !iso8601Parts[12] ? 0 : null;
}
if (timezone === 'z' || timezone === 'Z') {
return 0; The following two dates are both valid ISO8601 dates, but > new Date('2015-01-01 12:00')
Thu Jan 01 2015 12:00:00 GMT+0900 (JST)
> new Date('2015-01-01')
Thu Jan 01 2015 09:00:00 GMT+0900 (JST) |
@chriso tests pass with the patch |
The validator accepts ISO 8601 dates which include "%Y-%m-%d" and "%Y-%m-%d %H:%M". The validator makes use of Date.parse() to parse the date before checking if the date is invalid (e.g. 2015-02-29). The built-in Date.parse() function interprets "%Y-%m-%d" dates in GMT but "%Y-%m-%d %H:%M" in the user's timezone. This caused the validator to apply an incorrect timezone offset which then caused it to get out of sync with the built-in Date.parse() function. Because of this mismatch, certain dates such as 2011-12-21 were being marked as invalid when the user had a timezone offset < 0, e.g. in PST, since the date would become 2011-12-20 after the incorrect timezone offset was applied. This fixes the function that determines the timezone in the date. It now checks for the special "%Y-%m-%d" case and returns a timezone offset of 0 (GMT). cc #480
Thanks. I've published the fix in |
Thanks I'll grab the new version and let you know if I run into any other issues :) |
Awesome, thanks! |
Was confused by the docs which said isDate takes a string. Took some time to figure out correct way. Hoping this helps others avoid that.