jws-frontend/src/utils/datetime.ts
Net 0986200910
feat: adjust payment page (#31)
* feat: add installment no label

* feat: update types

* refactor: add i18n

* refactor: view receipt

* refactor: add type

* refactor: add dateFormatTh

* fixup! refactor: add i18n

* fixup! refactor: add dateFormatTh

* refactor: use dateFormatJS in monthDisplay

* refactor: handle year th-TH

* ลบ log

* refactor: handle color view mod
2024-10-31 10:00:35 +07:00

158 lines
3.9 KiB
TypeScript

import moment from 'moment';
import 'moment/dist/locale/th';
import 'moment/dist/locale/en-gb';
import { useI18n } from 'vue-i18n';
moment.locale('en-gb');
export function setLocale(locale: string) {
moment.locale(locale);
}
export function dateFormatJS(opts: {
date: string | Date | null;
locale?: string;
dayStyle?: 'numeric' | '2-digit';
monthStyle?: 'numeric' | '2-digit' | 'long' | 'short';
timeStyle?: 'full' | 'long' | 'medium' | 'short';
}) {
const dateObject = opts.date ? new Date(opts.date) : new Date();
const dateFormat = new Date(
Date.UTC(
dateObject.getUTCFullYear(),
dateObject.getUTCMonth(),
dateObject.getUTCDate(),
dateObject.getUTCHours(),
dateObject.getUTCMinutes(),
dateObject.getUTCSeconds(),
),
);
let formattedDate = new Intl.DateTimeFormat(opts.locale || 'en-US', {
day: opts.dayStyle,
month: opts.monthStyle,
timeStyle: opts.timeStyle,
year: 'numeric',
}).format(dateFormat);
if (opts.locale === 'th-Th') {
formattedDate = formattedDate.replace(/(\d{4})/, (year) =>
(parseInt(year) - 543).toString(),
);
}
return formattedDate;
}
/**
* @deprecated Please use dateFormatJS.
*/
export function dateFormat(
date?: string | Date | null,
fullmonth = false,
time = false,
number = false,
days = false,
) {
const m = moment(date);
if (!m.isValid()) return '';
if (time) return m.format('HH:mm');
if (number) {
const formattedNumberDate = m.format('L');
return formattedNumberDate;
}
const monthFormat = fullmonth ? 'MMMM' : 'MMM';
const formattedDate = m.format(
` ${days ? '' : 'DD'} ${monthFormat} YYYY ${time ? ' HH:mm' : ''}`,
);
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)
);
}
export function calculateAge(birthDate: Date | null | string): string;
export function calculateAge(
birthDate: Date | null | string,
only: 'year' | 'months' | 'days',
): number;
export function calculateAge(
birthDate: Date | null | string,
only?: 'year' | 'months' | 'days',
) {
if (!birthDate) return null;
const birthDateTimeStamp = new Date(birthDate).getTime();
const now = new Date();
const diff = now.getTime() - birthDateTimeStamp;
const ageDate = new Date(diff);
const years = ageDate.getUTCFullYear() - 1970;
const months = ageDate.getUTCMonth();
const days = ageDate.getUTCDate() - 1;
if (only) {
return only === 'year' ? years : only === 'months' ? months : days;
}
const { locale } = useI18n();
if (locale.value === 'tha') {
return `${years} ปี ${months !== 0 ? months + ' เดือน' : ''} ${days !== 0 ? days + ' วัน' : ''} `;
} else {
return `${years} years ${months !== 0 ? months + ' months' : ''} ${days !== 0 ? days + ' days' : ''} `;
}
}
export function disabledAfterToday(date: Date) {
const today = new Date();
today.setHours(0, 0, 0, 0);
return date > today;
}
export function parseAndFormatDate(
value: string | number | undefined,
locale: string,
) {
if (!value) return;
if (value && value.toString().length === 10) {
const [date, month, year] = value.toString().split('/');
if (locale === 'tha') {
const adjustedYear = Number(year) - 543;
return new Date(`${adjustedYear}-${month}-${date}T00:00:00.000Z`);
} else {
return new Date(`${year}-${month}-${date}T00:00:00.000Z`);
}
}
return null;
}