parseDate.js 769 B

12345678910111213141516171819202122232425
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseDate;
  6. /**
  7. * @flow
  8. */
  9. function parseDate(iso8601 /*: string*/) /*: ?Date*/{
  10. 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$');
  11. const match = regexp.exec(iso8601);
  12. if (!match) {
  13. return null;
  14. }
  15. const year = parseInt(match[1]) || 0;
  16. const month = (parseInt(match[2]) || 1) - 1;
  17. const day = parseInt(match[3]) || 0;
  18. const hour = parseInt(match[4]) || 0;
  19. const minute = parseInt(match[5]) || 0;
  20. const second = parseInt(match[6]) || 0;
  21. const milli = parseInt(match[8]) || 0;
  22. return new Date(Date.UTC(year, month, day, hour, minute, second, milli));
  23. }