parseDate.js 979 B

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