MCPcopy
hub / github.com/date-fns/date-fns / formatISO9075

Function formatISO9075

pkgs/core/src/formatISO9075/index.ts:47–89  ·  view source on GitHub ↗
(
  date: DateArg<Date> & {},
  options?: FormatISO9075Options,
)

Source from the content-addressed store, hash-verified

45 * //=> '19:00:52'
46 */
47export function formatISO9075(
48 date: DateArg<Date> & {},
49 options?: FormatISO9075Options,
50): string {
51 const date_ = toDate(date, options?.in);
52
53 if (!isValid(date_)) {
54 throw new RangeError("Invalid time value");
55 }
56
57 const format = options?.format ?? "extended";
58 const representation = options?.representation ?? "complete";
59
60 let result = "";
61
62 const dateDelimiter = format === "extended" ? "-" : "";
63 const timeDelimiter = format === "extended" ? ":" : "";
64
65 // Representation is either 'date' or 'complete'
66 if (representation !== "time") {
67 const day = addLeadingZeros(date_.getDate(), 2);
68 const month = addLeadingZeros(date_.getMonth() + 1, 2);
69 const year = addLeadingZeros(date_.getFullYear(), 4);
70
71 // yyyyMMdd or yyyy-MM-dd.
72 result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
73 }
74
75 // Representation is either 'time' or 'complete'
76 if (representation !== "date") {
77 const hour = addLeadingZeros(date_.getHours(), 2);
78 const minute = addLeadingZeros(date_.getMinutes(), 2);
79 const second = addLeadingZeros(date_.getSeconds(), 2);
80
81 // If there's also date, separate it with time with a space
82 const separator = result === "" ? "" : " ";
83
84 // HHmmss or HH:mm:ss.
85 result = `${result}${separator}${hour}${timeDelimiter}${minute}${timeDelimiter}${second}`;
86 }
87
88 return result;
89}

Callers 1

test.tsFile · 0.90

Calls 3

toDateFunction · 0.90
isValidFunction · 0.90
addLeadingZerosFunction · 0.90

Tested by

no test coverage detected