feat: update datetime utility function
This commit is contained in:
parent
65e32f85d8
commit
23e05efd24
1 changed files with 11 additions and 1 deletions
|
|
@ -15,6 +15,7 @@ export function dateFormatJS(opts: {
|
||||||
dayStyle?: 'numeric' | '2-digit';
|
dayStyle?: 'numeric' | '2-digit';
|
||||||
monthStyle?: 'numeric' | '2-digit' | 'long' | 'short';
|
monthStyle?: 'numeric' | '2-digit' | 'long' | 'short';
|
||||||
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
||||||
|
timeOnly?: boolean;
|
||||||
noDay?: boolean;
|
noDay?: boolean;
|
||||||
noMonth?: boolean;
|
noMonth?: boolean;
|
||||||
noYear?: boolean;
|
noYear?: boolean;
|
||||||
|
|
@ -24,7 +25,14 @@ export function dateFormatJS(opts: {
|
||||||
const { locale } = i18n.global;
|
const { locale } = i18n.global;
|
||||||
|
|
||||||
if (!opts.locale) {
|
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, {
|
let formatted = new Intl.DateTimeFormat(opts.locale, {
|
||||||
|
|
@ -35,6 +43,8 @@ export function dateFormatJS(opts: {
|
||||||
}).format(dt);
|
}).format(dt);
|
||||||
|
|
||||||
switch (opts.locale) {
|
switch (opts.locale) {
|
||||||
|
case Lang.Thai:
|
||||||
|
case 'th-TH':
|
||||||
case 'th-Th':
|
case 'th-Th':
|
||||||
return formatted.replace(/(\d{4})/, (year) =>
|
return formatted.replace(/(\d{4})/, (year) =>
|
||||||
(parseInt(year) - 543).toString(),
|
(parseInt(year) - 543).toString(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue