formatISO.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. exports.formatISO = formatISO;
  3. var _index = require("./toDate.js");
  4. var _index2 = require("./_lib/addLeadingZeros.js");
  5. /**
  6. * The {@link formatISO} function options.
  7. */
  8. /**
  9. * @name formatISO
  10. * @category Common Helpers
  11. * @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).
  12. *
  13. * @description
  14. * Return the formatted date string in ISO 8601 format. Options may be passed to control the parts and notations of the date.
  15. *
  16. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  17. *
  18. * @param date - The original date
  19. * @param options - An object with options.
  20. *
  21. * @returns The formatted date string (in loca.l time zone)
  22. *
  23. * @throws `date` must not be Invalid Date
  24. *
  25. * @example
  26. * // Represent 18 September 2019 in ISO 8601 format (local time zone is UTC):
  27. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52))
  28. * //=> '2019-09-18T19:00:52Z'
  29. *
  30. * @example
  31. * // Represent 18 September 2019 in ISO 8601, short format (local time zone is UTC):
  32. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
  33. * //=> '20190918T190052'
  34. *
  35. * @example
  36. * // Represent 18 September 2019 in ISO 8601 format, date only:
  37. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
  38. * //=> '2019-09-18'
  39. *
  40. * @example
  41. * // Represent 18 September 2019 in ISO 8601 format, time only (local time zone is UTC):
  42. * const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  43. * //=> '19:00:52Z'
  44. */
  45. function formatISO(date, options) {
  46. const _date = (0, _index.toDate)(date);
  47. if (isNaN(_date.getTime())) {
  48. throw new RangeError("Invalid time value");
  49. }
  50. const format = options?.format ?? "extended";
  51. const representation = options?.representation ?? "complete";
  52. let result = "";
  53. let tzOffset = "";
  54. const dateDelimiter = format === "extended" ? "-" : "";
  55. const timeDelimiter = format === "extended" ? ":" : "";
  56. // Representation is either 'date' or 'complete'
  57. if (representation !== "time") {
  58. const day = (0, _index2.addLeadingZeros)(_date.getDate(), 2);
  59. const month = (0, _index2.addLeadingZeros)(_date.getMonth() + 1, 2);
  60. const year = (0, _index2.addLeadingZeros)(_date.getFullYear(), 4);
  61. // yyyyMMdd or yyyy-MM-dd.
  62. result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
  63. }
  64. // Representation is either 'time' or 'complete'
  65. if (representation !== "date") {
  66. // Add the timezone.
  67. const offset = _date.getTimezoneOffset();
  68. if (offset !== 0) {
  69. const absoluteOffset = Math.abs(offset);
  70. const hourOffset = (0, _index2.addLeadingZeros)(
  71. Math.trunc(absoluteOffset / 60),
  72. 2,
  73. );
  74. const minuteOffset = (0, _index2.addLeadingZeros)(absoluteOffset % 60, 2);
  75. // If less than 0, the sign is +, because it is ahead of time.
  76. const sign = offset < 0 ? "+" : "-";
  77. tzOffset = `${sign}${hourOffset}:${minuteOffset}`;
  78. } else {
  79. tzOffset = "Z";
  80. }
  81. const hour = (0, _index2.addLeadingZeros)(_date.getHours(), 2);
  82. const minute = (0, _index2.addLeadingZeros)(_date.getMinutes(), 2);
  83. const second = (0, _index2.addLeadingZeros)(_date.getSeconds(), 2);
  84. // If there's also date, separate it with time with 'T'
  85. const separator = result === "" ? "" : "T";
  86. // Creates a time string consisting of hour, minute, and second, separated by delimiters, if defined.
  87. const time = [hour, minute, second].join(timeDelimiter);
  88. // HHmmss or HH:mm:ss.
  89. result = `${result}${separator}${time}${tzOffset}`;
  90. }
  91. return result;
  92. }