formatISO.cjs 3.5 KB

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