index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import toDate from "../toDate/index.js";
  2. import isValid from "../isValid/index.js";
  3. import addLeadingZeros from "../_lib/addLeadingZeros/index.js";
  4. /**
  5. * @name formatISO
  6. * @category Common Helpers
  7. * @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).
  8. *
  9. * @description
  10. * Return the formatted date string in ISO 8601 format. Options may be passed to control the parts and notations of the date.
  11. *
  12. * @param {Date|Number} date - the original date
  13. * @param {Object} [options] - an object with options.
  14. * @param {'extended'|'basic'} [options.format='extended'] - if 'basic', hide delimiters between date and time values.
  15. * @param {'complete'|'date'|'time'} [options.representation='complete'] - format date, time with time zone, or both.
  16. * @returns {String} the formatted date string
  17. * @throws {TypeError} 1 argument required
  18. * @throws {RangeError} `date` must not be Invalid Date
  19. * @throws {RangeError} `options.format` must be 'extended' or 'basic'
  20. * @throws {RangeError} `options.represenation` must be 'date', 'time' or 'complete'
  21. *
  22. * @example
  23. * // Represent 18 September 2019 in ISO 8601 format (UTC):
  24. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52))
  25. * //=> '2019-09-18T19:00:52Z'
  26. *
  27. * @example
  28. * // Represent 18 September 2019 in ISO 8601, short format (UTC):
  29. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
  30. * //=> '20190918T190052'
  31. *
  32. * @example
  33. * // Represent 18 September 2019 in ISO 8601 format, date only:
  34. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
  35. * //=> '2019-09-18'
  36. *
  37. * @example
  38. * // Represent 18 September 2019 in ISO 8601 format, time only (UTC):
  39. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  40. * //=> '19:00:52Z'
  41. */
  42. export default function formatISO(dirtyDate, dirtyOptions) {
  43. if (arguments.length < 1) {
  44. throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
  45. }
  46. var originalDate = toDate(dirtyDate);
  47. if (!isValid(originalDate)) {
  48. throw new RangeError('Invalid time value');
  49. }
  50. var options = dirtyOptions || {};
  51. var format = options.format == null ? 'extended' : String(options.format);
  52. var representation = options.representation == null ? 'complete' : String(options.representation);
  53. if (format !== 'extended' && format !== 'basic') {
  54. throw new RangeError("format must be 'extended' or 'basic'");
  55. }
  56. if (representation !== 'date' && representation !== 'time' && representation !== 'complete') {
  57. throw new RangeError("representation must be 'date', 'time', or 'complete'");
  58. }
  59. var result = '';
  60. var tzOffset = '';
  61. var dateDelimiter = format === 'extended' ? '-' : '';
  62. var timeDelimiter = format === 'extended' ? ':' : ''; // Representation is either 'date' or 'complete'
  63. if (representation !== 'time') {
  64. var day = addLeadingZeros(originalDate.getDate(), 2);
  65. var month = addLeadingZeros(originalDate.getMonth() + 1, 2);
  66. var year = addLeadingZeros(originalDate.getFullYear(), 4); // yyyyMMdd or yyyy-MM-dd.
  67. result = "".concat(year).concat(dateDelimiter).concat(month).concat(dateDelimiter).concat(day);
  68. } // Representation is either 'time' or 'complete'
  69. if (representation !== 'date') {
  70. // Add the timezone.
  71. var offset = originalDate.getTimezoneOffset();
  72. if (offset !== 0) {
  73. var absoluteOffset = Math.abs(offset);
  74. var hourOffset = addLeadingZeros(Math.floor(absoluteOffset / 60), 2);
  75. var minuteOffset = addLeadingZeros(absoluteOffset % 60, 2); // If less than 0, the sign is +, because it is ahead of time.
  76. var sign = offset < 0 ? '+' : '-';
  77. tzOffset = "".concat(sign).concat(hourOffset, ":").concat(minuteOffset);
  78. } else {
  79. tzOffset = 'Z';
  80. }
  81. var hour = addLeadingZeros(originalDate.getHours(), 2);
  82. var minute = addLeadingZeros(originalDate.getMinutes(), 2);
  83. var second = addLeadingZeros(originalDate.getSeconds(), 2); // If there's also date, separate it with time with 'T'
  84. var separator = result === '' ? '' : 'T'; // Creates a time string consisting of hour, minute, and second, separated by delimiters, if defined.
  85. var time = [hour, minute, second].join(timeDelimiter); // HHmmss or HH:mm:ss.
  86. result = "".concat(result).concat(separator).concat(time).concat(tzOffset);
  87. }
  88. return result;
  89. }