UNPKG

3.01 kBJavaScriptView Raw
1import { isValid } from "./isValid.mjs";
2import { toDate } from "./toDate.mjs";
3import { addLeadingZeros } from "./_lib/addLeadingZeros.mjs";
4
5/**
6 * The {@link formatISO9075} function options.
7 */
8
9/**
10 * @name formatISO9075
11 * @category Common Helpers
12 * @summary Format the date according to the ISO 9075 standard (https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_get-format).
13 *
14 * @description
15 * Return the formatted date string in ISO 9075 format. Options may be passed to control the parts and notations of the date.
16 *
17 * @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).
18 *
19 * @param date - The original date
20 * @param options - An object with options.
21 *
22 * @returns The formatted date string
23 *
24 * @throws `date` must not be Invalid Date
25 *
26 * @example
27 * // Represent 18 September 2019 in ISO 9075 format:
28 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52))
29 * //=> '2019-09-18 19:00:52'
30 *
31 * @example
32 * // Represent 18 September 2019 in ISO 9075, short format:
33 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
34 * //=> '20190918 190052'
35 *
36 * @example
37 * // Represent 18 September 2019 in ISO 9075 format, date only:
38 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
39 * //=> '2019-09-18'
40 *
41 * @example
42 * // Represent 18 September 2019 in ISO 9075 format, time only:
43 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
44 * //=> '19:00:52'
45 */
46export function formatISO9075(date, options) {
47 const _date = toDate(date);
48
49 if (!isValid(_date)) {
50 throw new RangeError("Invalid time value");
51 }
52
53 const format = options?.format ?? "extended";
54 const representation = options?.representation ?? "complete";
55
56 let result = "";
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 const hour = addLeadingZeros(_date.getHours(), 2);
74 const minute = addLeadingZeros(_date.getMinutes(), 2);
75 const second = addLeadingZeros(_date.getSeconds(), 2);
76
77 // If there's also date, separate it with time with a space
78 const separator = result === "" ? "" : " ";
79
80 // HHmmss or HH:mm:ss.
81 result = `${result}${separator}${hour}${timeDelimiter}${minute}${timeDelimiter}${second}`;
82 }
83
84 return result;
85}
86
87// Fallback for modularized imports:
88export default formatISO9075;