Ideally, the output structure should emit a type field to show if pattern is really a date or not. Something like this:
type FormatDateInfo = {
// new date type field
type: "date" | "time" | "datetime" | "none",
clockType: 12 | 24,
seconds: boolean,
minutes: boolean,
hours: boolean,
day: boolean,
month: boolean,
year: boolean,
}
This has the benefit that:
- You don't have to call
isDateFormat to determine if something is a date beforehand.
- You can pass the
info.time into a map of default patterns:
const ISO_PATTERNS = {
date: "yyyy-mm-dd",
time: "hh:mm:ss",
datetime: "yyyy-mm-ddThh:mm:ss",
none: "General",
};
// return an ISO formatted date, or a general formatted number, based on a cell format pattern:
const info = getFormatDateInfo(pattern);
const normalizedPattern = ISO_PATTERNS[info.type];
return format(normalizedPattern, value);
Alternatively, we might consider a isDate + granularity approach:
type FormatDateInfo = {
// new "is a date or not" field
date: boolean,
// new granularity field
granularity: "year" | "month" | "day" | "hours" | "minutes" | "seconds" | "none",
...
}
This does make it impossible to differentiate the "time" type though, so maybe it makes sense to do both.
Ideally, the output structure should emit a type field to show if pattern is really a date or not. Something like this:
This has the benefit that:
isDateFormatto determine if something is a date beforehand.info.timeinto a map of default patterns:Alternatively, we might consider a
isDate+granularityapproach:This does make it impossible to differentiate the "time" type though, so maybe it makes sense to do both.