From 61ee19c59c7195d563606d7a5499c09e81853f16 Mon Sep 17 00:00:00 2001 From: Jackson Currie Date: Tue, 18 Feb 2025 15:49:07 +1300 Subject: [PATCH] Fix javascript time format --- web/system_date_time_format.js | 25 +++++++++++++++++++++++++ web/system_date_time_format.min.js | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 web/system_date_time_format.js diff --git a/web/system_date_time_format.js b/web/system_date_time_format.js new file mode 100644 index 0000000..40e0a12 --- /dev/null +++ b/web/system_date_time_format.js @@ -0,0 +1,25 @@ +const getDateTimeFormatPattern = (options) => { + const parts = new Intl.DateTimeFormat(void 0, options).formatToParts(new Date()); + const hasDayPeriod = parts.some(part => part.type == "dayPeriod"); + return parts.map(part => { + switch (part.type) { + case "weekday": return "EEEE"; + case "day": return "d".repeat(part.value.length); + case "month": return "M".repeat(Math.min(4, part.value.length)); + case "year": return "y".repeat(part.value.length); + case "hour": return hasDayPeriod ? "h" : "HH"; + case "minute": return "m".repeat(part.value.length); + case "second": return "s".repeat(part.value.length); + case "literal": return part.value; + case "dayPeriod": return "a"; + default: return ""; + } + }) + .join(""); +}; + +const getDateFormat = () => getDateTimeFormatPattern({ dateStyle: "short" }); +const getMediumDateFormat = () => getDateTimeFormatPattern({ dateStyle: "medium" }); +const getLongDateFormat = () => getDateTimeFormatPattern({ dateStyle: "long" }); +const getFullDateFormat = () => getDateTimeFormatPattern({ dateStyle: "full" }); +const getTimeFormat = () => getDateTimeFormatPattern({ timeStyle: "short" }); diff --git a/web/system_date_time_format.min.js b/web/system_date_time_format.min.js index 403e0d4..d4b3718 100644 --- a/web/system_date_time_format.min.js +++ b/web/system_date_time_format.min.js @@ -1 +1 @@ -const getDateTimeFormatPattern=e=>new Intl.DateTimeFormat(void 0,e).formatToParts(new Date).map((e=>{switch(e.type){case"weekday":return"EEEE";case"day":return"d".repeat(e.value.length);case"month":return"M".repeat(Math.min(4,e.value.length));case"year":return"y".repeat(e.value.length);case"hour":return"h".repeat(e.value.length);case"minute":return"m".repeat(e.value.length);case"second":return"s".repeat(e.value.length);case"literal":return e.value;default:return""}})).join("");getDateFormat=()=>getDateTimeFormatPattern({dateStyle:"short"}),getMediumDateFormat=()=>getDateTimeFormatPattern({dateStyle:"medium"}),getLongDateFormat=()=>getDateTimeFormatPattern({dateStyle:"long"}),getFullDateFormat=()=>getDateTimeFormatPattern({dateStyle:"full"}),getTimeFormat=()=>getDateTimeFormatPattern({timeStyle:"short"}); \ No newline at end of file +const getDateTimeFormatPattern=e=>{let t=new Intl.DateTimeFormat(void 0,e).formatToParts(new Date),a=t.some(e=>"dayPeriod"==e.type);return t.map(e=>{switch(e.type){case"weekday":return"EEEE";case"day":return"d".repeat(e.value.length);case"month":return"M".repeat(Math.min(4,e.value.length));case"year":return"y".repeat(e.value.length);case"hour":return a?"h":"HH";case"minute":return"m".repeat(e.value.length);case"second":return"s".repeat(e.value.length);case"literal":return e.value;case"dayPeriod":return"a";default:return""}}).join("")},getDateFormat=()=>getDateTimeFormatPattern({dateStyle:"short"}),getMediumDateFormat=()=>getDateTimeFormatPattern({dateStyle:"medium"}),getLongDateFormat=()=>getDateTimeFormatPattern({dateStyle:"long"}),getFullDateFormat=()=>getDateTimeFormatPattern({dateStyle:"full"}),getTimeFormat=()=>getDateTimeFormatPattern({timeStyle:"short"}); \ No newline at end of file