index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = formatISO;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../isValid/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/addLeadingZeros/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * @name formatISO
  12. * @category Common Helpers
  13. * @summary Format the date according to the ISO 8601 standard (http://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 time zone, or both.
  22. * @returns {String} the formatted date string
  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.represenation` must be 'date', 'time' or 'complete'
  27. *
  28. * @example
  29. * // Represent 18 September 2019 in ISO 8601 format (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 (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 (UTC):
  45. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  46. * //=> '19:00:52Z'
  47. */
  48. function formatISO(dirtyDate, dirtyOptions) {
  49. if (arguments.length < 1) {
  50. throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
  51. }
  52. var originalDate = (0, _index.default)(dirtyDate);
  53. if (!(0, _index2.default)(originalDate)) {
  54. throw new RangeError('Invalid time value');
  55. }
  56. var options = dirtyOptions || {};
  57. var format = options.format == null ? 'extended' : String(options.format);
  58. var representation = options.representation == null ? 'complete' : String(options.representation);
  59. if (format !== 'extended' && format !== 'basic') {
  60. throw new RangeError("format must be 'extended' or 'basic'");
  61. }
  62. if (representation !== 'date' && representation !== 'time' && representation !== 'complete') {
  63. throw new RangeError("representation must be 'date', 'time', or 'complete'");
  64. }
  65. var result = '';
  66. var tzOffset = '';
  67. var dateDelimiter = format === 'extended' ? '-' : '';
  68. var timeDelimiter = format === 'extended' ? ':' : ''; // Representation is either 'date' or 'complete'
  69. if (representation !== 'time') {
  70. var day = (0, _index3.default)(originalDate.getDate(), 2);
  71. var month = (0, _index3.default)(originalDate.getMonth() + 1, 2);
  72. var year = (0, _index3.default)(originalDate.getFullYear(), 4); // yyyyMMdd or yyyy-MM-dd.
  73. result = "".concat(year).concat(dateDelimiter).concat(month).concat(dateDelimiter).concat(day);
  74. } // Representation is either 'time' or 'complete'
  75. if (representation !== 'date') {
  76. // Add the timezone.
  77. var offset = originalDate.getTimezoneOffset();
  78. if (offset !== 0) {
  79. var absoluteOffset = Math.abs(offset);
  80. var hourOffset = (0, _index3.default)(Math.floor(absoluteOffset / 60), 2);
  81. var minuteOffset = (0, _index3.default)(absoluteOffset % 60, 2); // If less than 0, the sign is +, because it is ahead of time.
  82. var sign = offset < 0 ? '+' : '-';
  83. tzOffset = "".concat(sign).concat(hourOffset, ":").concat(minuteOffset);
  84. } else {
  85. tzOffset = 'Z';
  86. }
  87. var hour = (0, _index3.default)(originalDate.getHours(), 2);
  88. var minute = (0, _index3.default)(originalDate.getMinutes(), 2);
  89. var second = (0, _index3.default)(originalDate.getSeconds(), 2); // If there's also date, separate it with time with 'T'
  90. var separator = result === '' ? '' : 'T'; // Creates a time string consisting of hour, minute, and second, separated by delimiters, if defined.
  91. var time = [hour, minute, second].join(timeDelimiter); // HHmmss or HH:mm:ss.
  92. result = "".concat(result).concat(separator).concat(time).concat(tzOffset);
  93. }
  94. return result;
  95. }
  96. module.exports = exports.default;