formatISO.js 3.4 KB

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