UNPKG

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