163 lines
3.9 KiB
TypeScript
163 lines
3.9 KiB
TypeScript
import moment from 'moment';
|
|
import 'moment/dist/locale/th';
|
|
import 'moment/dist/locale/en-gb';
|
|
import { i18n } from 'src/boot/i18n';
|
|
import { Lang } from './ui';
|
|
|
|
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(),
|
|
),
|
|
);
|
|
|
|
const { locale } = i18n.global;
|
|
|
|
if (!opts.locale) {
|
|
opts.locale = locale.value === Lang.Thai ? 'th-Th' : 'en-US';
|
|
}
|
|
|
|
let formattedDate = new Intl.DateTimeFormat(opts.locale, {
|
|
day: opts.dayStyle || 'numeric',
|
|
month: opts.monthStyle || 'short',
|
|
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;
|
|
|
|
switch (only) {
|
|
case 'year':
|
|
return years;
|
|
case 'months':
|
|
return months;
|
|
case 'days':
|
|
return days;
|
|
default:
|
|
}
|
|
|
|
switch (i18n.global.locale.value) {
|
|
case Lang.Thai:
|
|
return `${years} ปี ${months !== 0 ? months + ' เดือน' : ''} ${days !== 0 ? days + ' วัน' : ''}`;
|
|
default:
|
|
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) {
|
|
if (!value) return;
|
|
|
|
if (value && value.toString().length === 10) {
|
|
const [date, month, year] = value.toString().split('/');
|
|
return new Date(`${year}-${month}-${date}T00:00:00.000Z`);
|
|
}
|
|
return null;
|
|
}
|