UNPKG

3.7 kBJavaScriptView Raw
1import { toDate } from "./toDate.mjs";
2import { addLeadingZeros } from "./_lib/addLeadingZeros.mjs";
3
4/**
5 * The {@link formatISO} function options.
6 */
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 */
45export function formatISO(date, options) {
46 const _date = toDate(date);
47
48 if (isNaN(_date.getTime())) {
49 throw new RangeError("Invalid time value");
50 }
51
52 const format = options?.format ?? "extended";
53 const representation = options?.representation ?? "complete";
54
55 let result = "";
56 let tzOffset = "";
57
58 const dateDelimiter = format === "extended" ? "-" : "";
59 const timeDelimiter = format === "extended" ? ":" : "";
60
61 // Representation is either 'date' or 'complete'
62 if (representation !== "time") {
63 const day = addLeadingZeros(_date.getDate(), 2);
64 const month = addLeadingZeros(_date.getMonth() + 1, 2);
65 const year = addLeadingZeros(_date.getFullYear(), 4);
66
67 // yyyyMMdd or yyyy-MM-dd.
68 result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
69 }
70
71 // Representation is either 'time' or 'complete'
72 if (representation !== "date") {
73 // Add the timezone.
74 const offset = _date.getTimezoneOffset();
75
76 if (offset !== 0) {
77 const absoluteOffset = Math.abs(offset);
78 const hourOffset = addLeadingZeros(Math.trunc(absoluteOffset / 60), 2);
79 const minuteOffset = addLeadingZeros(absoluteOffset % 60, 2);
80 // If less than 0, the sign is +, because it is ahead of time.
81 const sign = offset < 0 ? "+" : "-";
82
83 tzOffset = `${sign}${hourOffset}:${minuteOffset}`;
84 } else {
85 tzOffset = "Z";
86 }
87
88 const hour = addLeadingZeros(_date.getHours(), 2);
89 const minute = addLeadingZeros(_date.getMinutes(), 2);
90 const second = addLeadingZeros(_date.getSeconds(), 2);
91
92 // If there's also date, separate it with time with 'T'
93 const separator = result === "" ? "" : "T";
94
95 // Creates a time string consisting of hour, minute, and second, separated by delimiters, if defined.
96 const time = [hour, minute, second].join(timeDelimiter);
97
98 // HHmmss or HH:mm:ss.
99 result = `${result}${separator}${time}${tzOffset}`;
100 }
101
102 return result;
103}
104
105// Fallback for modularized imports:
106export default formatISO;