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