parseDate.js 727 B

123456789101112131415161718192021
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseDate;
  6. function parseDate(iso8601) {
  7. const regexp = new RegExp('^([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})' + 'T' + '([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})' + '(.([0-9]+))?' + 'Z$');
  8. const match = regexp.exec(iso8601);
  9. if (!match) {
  10. return null;
  11. }
  12. const year = parseInt(match[1]) || 0;
  13. const month = (parseInt(match[2]) || 1) - 1;
  14. const day = parseInt(match[3]) || 0;
  15. const hour = parseInt(match[4]) || 0;
  16. const minute = parseInt(match[5]) || 0;
  17. const second = parseInt(match[6]) || 0;
  18. const milli = parseInt(match[8]) || 0;
  19. return new Date(Date.UTC(year, month, day, hour, minute, second, milli));
  20. }