index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = formatISO;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = _interopRequireDefault(require("../_lib/addLeadingZeros/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. /**
  11. * @name formatISO
  12. * @category Common Helpers
  13. * @summary Format the date according to the ISO 8601 standard (https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm).
  14. *
  15. * @description
  16. * Return the formatted date string in ISO 8601 format. Options may be passed to control the parts and notations of the date.
  17. *
  18. * @param {Date|Number} date - the original date
  19. * @param {Object} [options] - an object with options.
  20. * @param {'extended'|'basic'} [options.format='extended'] - if 'basic', hide delimiters between date and time values.
  21. * @param {'complete'|'date'|'time'} [options.representation='complete'] - format date, time with local time zone, or both.
  22. * @returns {String} the formatted date string (in local time zone)
  23. * @throws {TypeError} 1 argument required
  24. * @throws {RangeError} `date` must not be Invalid Date
  25. * @throws {RangeError} `options.format` must be 'extended' or 'basic'
  26. * @throws {RangeError} `options.representation` must be 'date', 'time' or 'complete'
  27. *
  28. * @example
  29. * // Represent 18 September 2019 in ISO 8601 format (local time zone is UTC):
  30. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52))
  31. * //=> '2019-09-18T19:00:52Z'
  32. *
  33. * @example
  34. * // Represent 18 September 2019 in ISO 8601, short format (local time zone is UTC):
  35. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
  36. * //=> '20190918T190052'
  37. *
  38. * @example
  39. * // Represent 18 September 2019 in ISO 8601 format, date only:
  40. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
  41. * //=> '2019-09-18'
  42. *
  43. * @example
  44. * // Represent 18 September 2019 in ISO 8601 format, time only (local time zone is UTC):
  45. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  46. * //=> '19:00:52Z'
  47. */
  48. function formatISO(date, options) {
  49. var _options$format, _options$representati;
  50. (0, _index3.default)(1, arguments);
  51. var originalDate = (0, _index.default)(date);
  52. if (isNaN(originalDate.getTime())) {
  53. throw new RangeError('Invalid time value');
  54. }
  55. var format = String((_options$format = options === null || options === void 0 ? void 0 : options.format) !== null && _options$format !== void 0 ? _options$format : 'extended');
  56. var representation = String((_options$representati = options === null || options === void 0 ? void 0 : options.representation) !== null && _options$representati !== void 0 ? _options$representati : 'complete');
  57. if (format !== 'extended' && format !== 'basic') {
  58. throw new RangeError("format must be 'extended' or 'basic'");
  59. }
  60. if (representation !== 'date' && representation !== 'time' && representation !== 'complete') {
  61. throw new RangeError("representation must be 'date', 'time', or 'complete'");
  62. }
  63. var result = '';
  64. var tzOffset = '';
  65. var dateDelimiter = format === 'extended' ? '-' : '';
  66. var timeDelimiter = format === 'extended' ? ':' : '';
  67. // Representation is either 'date' or 'complete'
  68. if (representation !== 'time') {
  69. var day = (0, _index2.default)(originalDate.getDate(), 2);
  70. var month = (0, _index2.default)(originalDate.getMonth() + 1, 2);
  71. var year = (0, _index2.default)(originalDate.getFullYear(), 4);
  72. // yyyyMMdd or yyyy-MM-dd.
  73. result = "".concat(year).concat(dateDelimiter).concat(month).concat(dateDelimiter).concat(day);
  74. }
  75. // Representation is either 'time' or 'complete'
  76. if (representation !== 'date') {
  77. // Add the timezone.
  78. var offset = originalDate.getTimezoneOffset();
  79. if (offset !== 0) {
  80. var absoluteOffset = Math.abs(offset);
  81. var hourOffset = (0, _index2.default)(Math.floor(absoluteOffset / 60), 2);
  82. var minuteOffset = (0, _index2.default)(absoluteOffset % 60, 2);
  83. // If less than 0, the sign is +, because it is ahead of time.
  84. var sign = offset < 0 ? '+' : '-';
  85. tzOffset = "".concat(sign).concat(hourOffset, ":").concat(minuteOffset);
  86. } else {
  87. tzOffset = 'Z';
  88. }
  89. var hour = (0, _index2.default)(originalDate.getHours(), 2);
  90. var minute = (0, _index2.default)(originalDate.getMinutes(), 2);
  91. var second = (0, _index2.default)(originalDate.getSeconds(), 2);
  92. // If there's also date, separate it with time with 'T'
  93. var separator = result === '' ? '' : 'T';
  94. // Creates a time string consisting of hour, minute, and second, separated by delimiters, if defined.
  95. var time = [hour, minute, second].join(timeDelimiter);
  96. // HHmmss or HH:mm:ss.
  97. result = "".concat(result).concat(separator).concat(time).concat(tzOffset);
  98. }
  99. return result;
  100. }
  101. module.exports = exports.default;