fix format date locale
This commit is contained in:
parent
3882678e9f
commit
6795453795
1 changed files with 60 additions and 4 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
import 'moment/dist/locale/th';
|
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
import 'moment/dist/locale/th';
|
||||||
|
import 'moment/dist/locale/en-gb';
|
||||||
|
|
||||||
moment.locale('th');
|
moment.locale('en-gb');
|
||||||
|
|
||||||
export function setLocale(locale: string) {
|
export function setLocale(locale: string) {
|
||||||
moment.locale(locale);
|
moment.locale(locale);
|
||||||
|
|
@ -11,12 +12,67 @@ export function dateFormat(
|
||||||
date?: string | Date | null,
|
date?: string | Date | null,
|
||||||
fullmonth = false,
|
fullmonth = false,
|
||||||
time = false,
|
time = false,
|
||||||
|
number = false,
|
||||||
) {
|
) {
|
||||||
const m = moment(date);
|
const m = moment(date);
|
||||||
|
|
||||||
if (!m.isValid()) return '';
|
if (!m.isValid()) return '';
|
||||||
|
|
||||||
const month = m.format(fullmonth ? 'MMMM' : 'MMM');
|
let yearOffset = 0;
|
||||||
|
if (moment.locale() === 'th') {
|
||||||
return m.format(`DD ${month} YYYY ${time ? ' HH:mm' : ''}`);
|
yearOffset = 543;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (time) return m.format('HH:mm');
|
||||||
|
if (number) {
|
||||||
|
let formattedNumberDate = m.format('L');
|
||||||
|
if (yearOffset) {
|
||||||
|
const adjustedYear = m.year() + yearOffset;
|
||||||
|
formattedNumberDate = formattedNumberDate.replace(
|
||||||
|
m.year().toString(),
|
||||||
|
adjustedYear.toString(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return formattedNumberDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
const monthFormat = fullmonth ? 'MMMM' : 'MMM';
|
||||||
|
const formattedDate = m.format(
|
||||||
|
`DD ${monthFormat} YYYY ${time ? ' HH:mm' : ''}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Adjust year for Buddhist calendar if locale is 'th'
|
||||||
|
if (yearOffset) {
|
||||||
|
const adjustedYear = m.year() + yearOffset;
|
||||||
|
return formattedDate.replace(m.year().toString(), adjustedYear.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return formattedDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pad(n: number) {
|
||||||
|
return `${Math.floor(Math.abs(n))}`.padStart(2, '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTimezoneOffset(date: Date) {
|
||||||
|
const tzOffset = -date.getTimezoneOffset();
|
||||||
|
const diff = tzOffset >= 0 ? '+' : '-';
|
||||||
|
return diff + pad(tzOffset / 60) + ':' + pad(tzOffset % 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toISOStringWithTimezone(date: Date) {
|
||||||
|
return (
|
||||||
|
date.getFullYear() +
|
||||||
|
'-' +
|
||||||
|
pad(date.getMonth() + 1) +
|
||||||
|
'-' +
|
||||||
|
pad(date.getDate()) +
|
||||||
|
'T' +
|
||||||
|
pad(date.getHours()) +
|
||||||
|
':' +
|
||||||
|
pad(date.getMinutes()) +
|
||||||
|
':' +
|
||||||
|
pad(date.getSeconds()) +
|
||||||
|
getTimezoneOffset(date)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue