|
470 | 470 | return pattern && pattern.test(str);
|
471 | 471 | };
|
472 | 472 |
|
| 473 | + function getTimezoneOffset(str) { |
| 474 | + var iso8601Parts = str.match(iso8601); |
| 475 | + if (!iso8601Parts) { |
| 476 | + return new Date().getTimezoneOffset(); |
| 477 | + } |
| 478 | + var timezone = iso8601Parts[21]; |
| 479 | + if (!timezone || timezone === 'z' || timezone === 'Z') { |
| 480 | + return 0; |
| 481 | + } |
| 482 | + var sign = iso8601Parts[22], hours, minutes; |
| 483 | + if (timezone.indexOf(':') !== -1) { |
| 484 | + hours = parseInt(iso8601Parts[23]); |
| 485 | + minutes = parseInt(iso8601Parts[24]); |
| 486 | + } else { |
| 487 | + hours = 0; |
| 488 | + minutes = parseInt(iso8601Parts[23]); |
| 489 | + } |
| 490 | + return (hours * 60 + minutes) * (sign === '-' ? 1 : -1); |
| 491 | + } |
| 492 | + |
473 | 493 | validator.isDate = function (str) {
|
474 | 494 | var normalizedDate = new Date((new Date(str)).toUTCString());
|
475 |
| - var regularDay = String(normalizedDate.getDate()); |
476 | 495 | var utcDay = String(normalizedDate.getUTCDate());
|
| 496 | + // normalizedDate is in the user's timezone. Apply the input |
| 497 | + // timezone offset to the date so that the year and day match |
| 498 | + // the input |
| 499 | + var timezoneDifference = normalizedDate.getTimezoneOffset() - |
| 500 | + getTimezoneOffset(str); |
| 501 | + normalizedDate = new Date(normalizedDate.getTime() + |
| 502 | + 60000 * timezoneDifference); |
| 503 | + var regularDay = String(normalizedDate.getDate()); |
477 | 504 | var dayOrYear, dayOrYearMatches, year;
|
478 | 505 | if (isNaN(Date.parse(normalizedDate))) {
|
479 | 506 | return false;
|
|
0 commit comments