parseDate.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseDate;
  6. /**
  7. * Copyright (c) 2015-present, Parse, LLC.
  8. * All rights reserved.
  9. *
  10. * This source code is licensed under the BSD-style license found in the
  11. * LICENSE file in the root directory of this source tree. An additional grant
  12. * of patent rights can be found in the PATENTS file in the same directory.
  13. *
  14. * @flow
  15. */
  16. function parseDate(iso8601
  17. /*: string*/
  18. )
  19. /*: ?Date*/
  20. {
  21. var 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$');
  22. var match = regexp.exec(iso8601);
  23. if (!match) {
  24. return null;
  25. }
  26. var year = parseInt(match[1]) || 0;
  27. var month = (parseInt(match[2]) || 1) - 1;
  28. var day = parseInt(match[3]) || 0;
  29. var hour = parseInt(match[4]) || 0;
  30. var minute = parseInt(match[5]) || 0;
  31. var second = parseInt(match[6]) || 0;
  32. var milli = parseInt(match[8]) || 0;
  33. return new Date(Date.UTC(year, month, day, hour, minute, second, milli));
  34. }