jws-frontend/src/utils/datetime.ts

118 lines
2.9 KiB
TypeScript
Raw Normal View History

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';
import 'moment/dist/locale/en-gb';
2024-07-16 03:23:45 +00:00
import { useI18n } from 'vue-i18n';
2024-04-02 11:02:16 +07:00
2024-06-27 04:59:11 +00:00
moment.locale('en-gb');
2024-04-02 11:02:16 +07:00
export function setLocale(locale: string) {
moment.locale(locale);
}
export function dateFormat(
date?: string | Date | null,
fullmonth = false,
time = false,
2024-06-27 04:59:11 +00:00
number = 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');
if (number) {
const formattedNumberDate = m.format('L');
2024-06-27 04:59:11 +00:00
return formattedNumberDate;
}
const monthFormat = fullmonth ? 'MMMM' : 'MMM';
const formattedDate = m.format(
`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);
}
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
export function calculateAge(birthDate: Date | null | string): string;
export function calculateAge(
birthDate: Date | null | string,
only: 'year' | 'months' | 'days',
): number;
2024-08-28 13:49:57 +07:00
export function calculateAge(
birthDate: Date | null | string,
only?: 'year' | 'months' | 'days',
) {
2024-07-16 03:23:45 +00:00
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;
2024-08-28 13:49:57 +07:00
if (only) {
return only === 'year' ? years : only === 'months' ? months : days;
}
const { locale } = useI18n();
2024-08-26 16:24:08 +07:00
if (locale.value === 'tha') {
2024-07-16 03:23:45 +00:00
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;
}
2024-07-17 10:48:56 +00:00
export function parseAndFormatDate(
value: string | number | undefined,
locale: string,
) {
if (!value) return;
2024-07-17 10:48:56 +00:00
if (value && value.toString().length === 10) {
2024-07-17 10:48:56 +00:00
const [date, month, year] = value.toString().split('/');
2024-08-26 16:24:08 +07:00
if (locale === 'tha') {
2024-07-17 10:48:56 +00:00
const adjustedYear = Number(year) - 543;
2024-08-29 13:28:18 +07:00
return new Date(`${adjustedYear}-${month}-${date}T00:00:00.000Z`);
2024-07-17 10:48:56 +00:00
} else {
2024-08-29 13:28:18 +07:00
return new Date(`${year}-${month}-${date}T00:00:00.000Z`);
2024-07-17 10:48:56 +00:00
}
}
2024-07-17 10:48:56 +00:00
return null;
}