From 23e05efd249d33ee29ec92993cc5943a0b679bfa Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 25 Dec 2024 13:25:17 +0700 Subject: [PATCH] feat: update datetime utility function --- src/utils/datetime.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/datetime.ts b/src/utils/datetime.ts index de14c2e1..6994869b 100644 --- a/src/utils/datetime.ts +++ b/src/utils/datetime.ts @@ -15,6 +15,7 @@ export function dateFormatJS(opts: { dayStyle?: 'numeric' | '2-digit'; monthStyle?: 'numeric' | '2-digit' | 'long' | 'short'; timeStyle?: 'full' | 'long' | 'medium' | 'short'; + timeOnly?: boolean; noDay?: boolean; noMonth?: boolean; noYear?: boolean; @@ -24,7 +25,14 @@ export function dateFormatJS(opts: { const { locale } = i18n.global; if (!opts.locale) { - opts.locale = locale.value === Lang.Thai ? 'th-Th' : 'en-US'; + opts.locale = locale.value === Lang.Thai ? 'th-TH' : 'en-US'; + } + + if (opts.timeOnly) { + opts.noDay = true; + opts.noMonth = true; + opts.noYear = true; + opts.timeStyle = opts.timeStyle || 'short'; } let formatted = new Intl.DateTimeFormat(opts.locale, { @@ -35,6 +43,8 @@ export function dateFormatJS(opts: { }).format(dt); switch (opts.locale) { + case Lang.Thai: + case 'th-TH': case 'th-Th': return formatted.replace(/(\d{4})/, (year) => (parseInt(year) - 543).toString(),