index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = parseJSON;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. /**
  10. * @name parseJSON
  11. * @category Common Helpers
  12. * @summary Parse a JSON date string
  13. *
  14. * @description
  15. * Converts a complete ISO date string in UTC time, the typical format for transmitting
  16. * a date in JSON, to a JavaScript `Date` instance.
  17. *
  18. * This is a minimal implementation for converting dates retrieved from a JSON API to
  19. * a `Date` instance which can be used with other functions in the `date-fns` library.
  20. * The following formats are supported:
  21. *
  22. * - `2000-03-15T05:20:10.123Z`: The output of `.toISOString()` and `JSON.stringify(new Date())`
  23. * - `2000-03-15T05:20:10Z`: Without milliseconds
  24. * - `2000-03-15T05:20:10+00:00`: With a zero offset, the default JSON encoded format in some other languages
  25. * - `2000-03-15T05:20:10+05:45`: With a positive or negative offset, the default JSON encoded format in some other languages
  26. * - `2000-03-15T05:20:10+0000`: With a zero offset without a colon
  27. * - `2000-03-15T05:20:10`: Without a trailing 'Z' symbol
  28. * - `2000-03-15T05:20:10.1234567`: Up to 7 digits in milliseconds field. Only first 3 are taken into account since JS does not allow fractional milliseconds
  29. * - `2000-03-15 05:20:10`: With a space instead of a 'T' separator for APIs returning a SQL date without reformatting
  30. *
  31. * For convenience and ease of use these other input types are also supported
  32. * via [toDate]{@link https://date-fns.org/docs/toDate}:
  33. *
  34. * - A `Date` instance will be cloned
  35. * - A `number` will be treated as a timestamp
  36. *
  37. * Any other input type or invalid date strings will return an `Invalid Date`.
  38. *
  39. * @param {String|Number|Date} argument A fully formed ISO8601 date string to convert
  40. * @returns {Date} the parsed date in the local time zone
  41. * @throws {TypeError} 1 argument required
  42. */
  43. function parseJSON(argument) {
  44. (0, _index2.default)(1, arguments);
  45. if (typeof argument === 'string') {
  46. var parts = argument.match(/(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{0,7}))?(?:Z|(.)(\d{2}):?(\d{2})?)?/);
  47. if (parts) {
  48. // Group 8 matches the sign
  49. return new Date(Date.UTC(+parts[1], +parts[2] - 1, +parts[3], +parts[4] - (+parts[9] || 0) * (parts[8] == '-' ? -1 : 1), +parts[5] - (+parts[10] || 0) * (parts[8] == '-' ? -1 : 1), +parts[6], +((parts[7] || '0') + '00').substring(0, 3)));
  50. }
  51. return new Date(NaN);
  52. }
  53. return (0, _index.default)(argument);
  54. }
  55. module.exports = exports.default;