Skip to content

Commit cfb93ef

Browse files
jacksoncurriejackson-bluelabNikoro
authored
Fix javascript time format (#26)
* Fix javascript time format --------- Co-authored-by: Jackson Currie <[email protected]> Co-authored-by: Dominik Krajcer <[email protected]>
1 parent 3247d22 commit cfb93ef

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

web/system_date_time_format.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const getDateTimeFormatPattern = (options) => {
2+
const parts = new Intl.DateTimeFormat(void 0, options).formatToParts(new Date());
3+
const hasDayPeriod = parts.some(part => part.type === "dayPeriod");
4+
return parts.map(part => {
5+
switch (part.type) {
6+
case "weekday": return "EEEE";
7+
case "day": return "d".repeat(part.value.length);
8+
case "month": return "M".repeat(Math.min(4, part.value.length));
9+
case "year": return "y".repeat(part.value.length);
10+
case "hour": return hasDayPeriod ? "h".repeat(part.value.length) : "H".repeat(part.value.length);
11+
case "minute": return "m".repeat(part.value.length);
12+
case "second": return "s".repeat(part.value.length);
13+
case "literal": return part.value;
14+
case "dayPeriod": return "a";
15+
default: return "";
16+
}
17+
})
18+
.join("");
19+
};
20+
21+
const getDateFormat = () => getDateTimeFormatPattern({ dateStyle: "short" });
22+
const getMediumDateFormat = () => getDateTimeFormatPattern({ dateStyle: "medium" });
23+
const getLongDateFormat = () => getDateTimeFormatPattern({ dateStyle: "long" });
24+
const getFullDateFormat = () => getDateTimeFormatPattern({ dateStyle: "full" });
25+
const getTimeFormat = () => getDateTimeFormatPattern({ timeStyle: "short" });

web/system_date_time_format.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)