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

Leap year and general date validation for isDate() #418

Merged
merged 1 commit into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,26 @@ describe('Validators', function () {
validator: 'isDate'
, valid: [
'2011-08-04'
, '2011-09-30'
, '04. 08. 2011.'
, '08/04/2011'
, '2011.08.04'
, '4. 8. 2011. GMT'
, '2. 28. 2011. GMT'
, '2. 29. 2008. GMT'
, '2. 29. 1988. GMT'
, '2011-08-04 12:00'
, '2/29/24'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails for me in UTC+1. rawDate is equal to Thu Feb 29 2024 00:00:00 GMT+0100 (CET) and normalizedDate is equal to Wed Feb 28 2024 23:00:00 GMT+0100 (CET).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I realized I've been doing date normalization the wrong way. There's built-in functions that use the UTC date. I've switch to using those. Tests should pass now.

Also, let me know if you'd like me to squash my commits.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks. Yeah that'd be great if you could squash them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

squashed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is still failing for me. normalizedDate is equal to Thu Feb 29 2024 00:00:00 GMT+0100 (CET) and so getUTCDate() returns 28. All tests pass if I change getUTCDate() to getDate().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Tests fail for me if I switch to getDate(). I'll look at this later today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still looking at this. I'll send out a diff soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriso Just updated the diff. It should work now. I tested it in over 15 time zones.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

, '2-29-24'
]
, invalid: [
'foo'
, '2011-foo-04'
, '2011-09-31'
, '2. 29. 1987. GMT'
, '2. 29. 2011. GMT'
, '2/29/25'
, '2-29-25'
, 'GMT'
]
});
Expand Down
28 changes: 27 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,33 @@
};

validator.isDate = function (str) {
return !isNaN(Date.parse(str));
var normalizedDate = new Date((new Date(str)).toUTCString());
var regularDay = String(normalizedDate.getDate());
var utcDay = String(normalizedDate.getUTCDate());
var dayOrYear, dayOrYearMatches, year;
if (isNaN(Date.parse(normalizedDate))) {
return false;
}
//check for valid double digits that could be late days
//check for all matches since a string like '12/23' is a valid date
dayOrYearMatches = str.match(/[23]\d(\D|$)/g);
if (!dayOrYearMatches) {
return true;
}
dayOrYear = dayOrYearMatches.map(function(match) {
return match.slice(0,2);
}).join('/');
year = String(normalizedDate.getFullYear()).slice(-2);
//local date and UTC date can differ, but both are valid, so check agains both
if (dayOrYear === regularDay || dayOrYear === utcDay || dayOrYear === year) {
return true;
} else if ((dayOrYear === (regularDay + '/' + year)) || (dayOrYear === (year + '/' + regularDay))) {
return true;
} else if ((dayOrYear === (utcDay + '/' + year)) || (dayOrYear === (year + '/' + utcDay))) {
return true;
} else {
return false;
}
};

validator.isAfter = function (str, date) {
Expand Down