2024-04-02 11:02:16 +07:00
|
|
|
import moment from 'moment';
|
2024-06-27 04:59:11 +00:00
|
|
|
import 'moment/dist/locale/th';
|
2024-12-16 16:27:09 +07:00
|
|
|
import { i18n } from 'src/boot/i18n';
|
|
|
|
|
import { Lang } from './ui';
|
2024-04-02 11:02:16 +07:00
|
|
|
|
2024-12-18 08:55:04 +07:00
|
|
|
moment.locale('en');
|
2024-04-02 11:02:16 +07:00
|
|
|
|
|
|
|
|
export function setLocale(locale: string) {
|
|
|
|
|
moment.locale(locale);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-31 10:00:35 +07:00
|
|
|
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';
|
|
|
|
|
}) {
|
2024-12-18 09:15:21 +07:00
|
|
|
const dt = opts.date ? new Date(opts.date) : new Date();
|
2024-10-31 10:00:35 +07:00
|
|
|
|
2024-12-16 16:27:09 +07:00
|
|
|
const { locale } = i18n.global;
|
|
|
|
|
|
|
|
|
|
if (!opts.locale) {
|
|
|
|
|
opts.locale = locale.value === Lang.Thai ? 'th-Th' : 'en-US';
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 09:15:21 +07:00
|
|
|
let formatted = new Intl.DateTimeFormat(opts.locale, {
|
2024-12-16 16:27:09 +07:00
|
|
|
day: opts.dayStyle || 'numeric',
|
|
|
|
|
month: opts.monthStyle || 'short',
|
2024-10-31 10:00:35 +07:00
|
|
|
timeStyle: opts.timeStyle,
|
|
|
|
|
year: 'numeric',
|
2024-12-18 09:15:21 +07:00
|
|
|
}).format(dt);
|
2024-10-31 10:00:35 +07:00
|
|
|
|
2024-12-18 09:15:21 +07:00
|
|
|
switch (opts.locale) {
|
|
|
|
|
case 'th-Th':
|
|
|
|
|
return formatted.replace(/(\d{4})/, (year) =>
|
|
|
|
|
(parseInt(year) - 543).toString(),
|
|
|
|
|
);
|
|
|
|
|
default:
|
|
|
|
|
return formatted;
|
2024-10-31 10:00:35 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @deprecated Please use dateFormatJS.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-02 11:02:16 +07:00
|
|
|
export function dateFormat(
|
|
|
|
|
date?: string | Date | null,
|
|
|
|
|
fullmonth = false,
|
|
|
|
|
time = false,
|
2024-06-27 04:59:11 +00:00
|
|
|
number = false,
|
2024-10-31 10:00:35 +07:00
|
|
|
days = false,
|
2024-04-02 11:02:16 +07:00
|
|
|
) {
|
|
|
|
|
const m = moment(date);
|
|
|
|
|
|
|
|
|
|
if (!m.isValid()) return '';
|
|
|
|
|
|
2024-06-27 04:59:11 +00:00
|
|
|
if (time) return m.format('HH:mm');
|
2024-12-18 09:15:21 +07:00
|
|
|
if (number) return m.format('L');
|
2024-06-27 04:59:11 +00:00
|
|
|
|
|
|
|
|
const monthFormat = fullmonth ? 'MMMM' : 'MMM';
|
|
|
|
|
const formattedDate = m.format(
|
2024-10-31 10:00:35 +07:00
|
|
|
` ${days ? '' : 'DD'} ${monthFormat} YYYY ${time ? ' HH:mm' : ''}`,
|
2024-06-27 04:59:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-04-02 11:02:16 +07:00
|
|
|
|
2024-06-27 04:59:11 +00:00
|
|
|
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)
|
|
|
|
|
);
|
2024-04-02 11:02:16 +07:00
|
|
|
}
|
2024-07-16 03:23:45 +00:00
|
|
|
|
2024-12-18 09:15:21 +07:00
|
|
|
export function calculateAge(dt: Date | null | string): string;
|
2024-10-03 18:10:40 +07:00
|
|
|
export function calculateAge(
|
2024-12-18 09:15:21 +07:00
|
|
|
dt: Date | null | string,
|
2024-10-03 18:10:40 +07:00
|
|
|
only: 'year' | 'months' | 'days',
|
|
|
|
|
): number;
|
2024-08-28 13:49:57 +07:00
|
|
|
export function calculateAge(
|
2024-12-18 09:15:21 +07:00
|
|
|
dt: Date | null | string,
|
2024-08-28 13:49:57 +07:00
|
|
|
only?: 'year' | 'months' | 'days',
|
|
|
|
|
) {
|
2024-12-18 09:15:21 +07:00
|
|
|
if (!dt) return null;
|
2024-07-16 03:23:45 +00:00
|
|
|
|
2024-12-18 09:15:21 +07:00
|
|
|
const diff = Date.now() - new Date(dt).getTime();
|
|
|
|
|
const offset = new Date(diff);
|
|
|
|
|
const years = offset.getUTCFullYear() - 1970;
|
|
|
|
|
const months = offset.getUTCMonth();
|
|
|
|
|
const days = offset.getUTCDate() - 1;
|
2024-07-16 03:23:45 +00:00
|
|
|
|
2024-12-17 15:01:00 +07:00
|
|
|
switch (only) {
|
|
|
|
|
case 'year':
|
|
|
|
|
return years;
|
|
|
|
|
case 'months':
|
|
|
|
|
return months;
|
|
|
|
|
case 'days':
|
|
|
|
|
return days;
|
|
|
|
|
default:
|
2024-08-28 13:49:57 +07:00
|
|
|
}
|
|
|
|
|
|
2024-12-17 15:01:00 +07:00
|
|
|
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' : ''} `;
|
2024-07-16 03:23:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function disabledAfterToday(date: Date) {
|
|
|
|
|
const today = new Date();
|
|
|
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
|
return date > today;
|
|
|
|
|
}
|
2024-07-16 08:12:01 +00:00
|
|
|
|
2024-12-18 08:58:29 +07:00
|
|
|
/**
|
|
|
|
|
* This function accept date format from dd/MMM/YYYY and convert into date object
|
|
|
|
|
*/
|
2024-11-05 07:30:03 +07:00
|
|
|
export function parseAndFormatDate(value: string | number | undefined) {
|
2024-07-16 08:12:01 +00:00
|
|
|
if (!value) return;
|
2024-07-17 10:48:56 +00:00
|
|
|
|
2024-07-16 08:12:01 +00:00
|
|
|
if (value && value.toString().length === 10) {
|
2024-07-17 10:48:56 +00:00
|
|
|
const [date, month, year] = value.toString().split('/');
|
2024-11-05 07:30:03 +07:00
|
|
|
return new Date(`${year}-${month}-${date}T00:00:00.000Z`);
|
2024-07-16 08:12:01 +00:00
|
|
|
}
|
2024-07-17 10:48:56 +00:00
|
|
|
return null;
|
2024-07-16 08:12:01 +00:00
|
|
|
}
|