isDate.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isDate;
  6. var _assertString = require('./util/assertString');
  7. var _assertString2 = _interopRequireDefault(_assertString);
  8. var _isISO = require('./isISO8601');
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function getTimezoneOffset(str) {
  11. var iso8601Parts = str.match(_isISO.iso8601);
  12. var timezone = void 0,
  13. sign = void 0,
  14. hours = void 0,
  15. minutes = void 0;
  16. if (!iso8601Parts) {
  17. str = str.toLowerCase();
  18. timezone = str.match(/(?:\s|gmt\s*)(-|\+)(\d{1,4})(\s|$)/);
  19. if (!timezone) {
  20. return str.indexOf('gmt') !== -1 ? 0 : null;
  21. }
  22. sign = timezone[1];
  23. var offset = timezone[2];
  24. if (offset.length === 3) {
  25. offset = '0' + offset;
  26. }
  27. if (offset.length <= 2) {
  28. hours = 0;
  29. minutes = parseInt(offset, 10);
  30. } else {
  31. hours = parseInt(offset.slice(0, 2), 10);
  32. minutes = parseInt(offset.slice(2, 4), 10);
  33. }
  34. } else {
  35. timezone = iso8601Parts[21];
  36. if (!timezone) {
  37. // if no hour/minute was provided, the date is GMT
  38. return !iso8601Parts[12] ? 0 : null;
  39. }
  40. if (timezone === 'z' || timezone === 'Z') {
  41. return 0;
  42. }
  43. sign = iso8601Parts[22];
  44. if (timezone.indexOf(':') !== -1) {
  45. hours = parseInt(iso8601Parts[23], 10);
  46. minutes = parseInt(iso8601Parts[24], 10);
  47. } else {
  48. hours = 0;
  49. minutes = parseInt(iso8601Parts[23], 10);
  50. }
  51. }
  52. return (hours * 60 + minutes) * (sign === '-' ? 1 : -1);
  53. }
  54. function isDate(str) {
  55. (0, _assertString2.default)(str);
  56. var normalizedDate = new Date(Date.parse(str));
  57. if (isNaN(normalizedDate)) {
  58. return false;
  59. }
  60. // normalizedDate is in the user's timezone. Apply the input
  61. // timezone offset to the date so that the year and day match
  62. // the input
  63. var timezoneOffset = getTimezoneOffset(str);
  64. if (timezoneOffset !== null) {
  65. var timezoneDifference = normalizedDate.getTimezoneOffset() - timezoneOffset;
  66. normalizedDate = new Date(normalizedDate.getTime() + 60000 * timezoneDifference);
  67. }
  68. var day = String(normalizedDate.getDate());
  69. var dayOrYear = void 0,
  70. dayOrYearMatches = void 0,
  71. year = void 0;
  72. // check for valid double digits that could be late days
  73. // check for all matches since a string like '12/23' is a valid date
  74. // ignore everything with nearby colons
  75. dayOrYearMatches = str.match(/(^|[^:\d])[23]\d([^T:\d]|$)/g);
  76. if (!dayOrYearMatches) {
  77. return true;
  78. }
  79. dayOrYear = dayOrYearMatches.map(function (digitString) {
  80. return digitString.match(/\d+/g)[0];
  81. }).join('/');
  82. year = String(normalizedDate.getFullYear()).slice(-2);
  83. if (dayOrYear === day || dayOrYear === year) {
  84. return true;
  85. } else if (dayOrYear === '' + day / year || dayOrYear === '' + year / day) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. module.exports = exports['default'];