chore: clean datetime function
This commit is contained in:
parent
f9f0d1d313
commit
75e36663c2
1 changed files with 20 additions and 36 deletions
|
|
@ -16,18 +16,7 @@ export function dateFormatJS(opts: {
|
||||||
monthStyle?: 'numeric' | '2-digit' | 'long' | 'short';
|
monthStyle?: 'numeric' | '2-digit' | 'long' | 'short';
|
||||||
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
||||||
}) {
|
}) {
|
||||||
const dateObject = opts.date ? new Date(opts.date) : new Date();
|
const dt = 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;
|
const { locale } = i18n.global;
|
||||||
|
|
||||||
|
|
@ -35,20 +24,21 @@ export function dateFormatJS(opts: {
|
||||||
opts.locale = locale.value === Lang.Thai ? 'th-Th' : 'en-US';
|
opts.locale = locale.value === Lang.Thai ? 'th-Th' : 'en-US';
|
||||||
}
|
}
|
||||||
|
|
||||||
let formattedDate = new Intl.DateTimeFormat(opts.locale, {
|
let formatted = new Intl.DateTimeFormat(opts.locale, {
|
||||||
day: opts.dayStyle || 'numeric',
|
day: opts.dayStyle || 'numeric',
|
||||||
month: opts.monthStyle || 'short',
|
month: opts.monthStyle || 'short',
|
||||||
timeStyle: opts.timeStyle,
|
timeStyle: opts.timeStyle,
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
}).format(dateFormat);
|
}).format(dt);
|
||||||
|
|
||||||
if (opts.locale === 'th-Th') {
|
switch (opts.locale) {
|
||||||
formattedDate = formattedDate.replace(/(\d{4})/, (year) =>
|
case 'th-Th':
|
||||||
(parseInt(year) - 543).toString(),
|
return formatted.replace(/(\d{4})/, (year) =>
|
||||||
);
|
(parseInt(year) - 543).toString(),
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
return formatted;
|
||||||
}
|
}
|
||||||
|
|
||||||
return formattedDate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -67,10 +57,7 @@ export function dateFormat(
|
||||||
if (!m.isValid()) return '';
|
if (!m.isValid()) return '';
|
||||||
|
|
||||||
if (time) return m.format('HH:mm');
|
if (time) return m.format('HH:mm');
|
||||||
if (number) {
|
if (number) return m.format('L');
|
||||||
const formattedNumberDate = m.format('L');
|
|
||||||
return formattedNumberDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
const monthFormat = fullmonth ? 'MMMM' : 'MMM';
|
const monthFormat = fullmonth ? 'MMMM' : 'MMM';
|
||||||
const formattedDate = m.format(
|
const formattedDate = m.format(
|
||||||
|
|
@ -107,25 +94,22 @@ export function toISOStringWithTimezone(date: Date) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calculateAge(birthDate: Date | null | string): string;
|
export function calculateAge(dt: Date | null | string): string;
|
||||||
export function calculateAge(
|
export function calculateAge(
|
||||||
birthDate: Date | null | string,
|
dt: Date | null | string,
|
||||||
only: 'year' | 'months' | 'days',
|
only: 'year' | 'months' | 'days',
|
||||||
): number;
|
): number;
|
||||||
export function calculateAge(
|
export function calculateAge(
|
||||||
birthDate: Date | null | string,
|
dt: Date | null | string,
|
||||||
only?: 'year' | 'months' | 'days',
|
only?: 'year' | 'months' | 'days',
|
||||||
) {
|
) {
|
||||||
if (!birthDate) return null;
|
if (!dt) return null;
|
||||||
|
|
||||||
const birthDateTimeStamp = new Date(birthDate).getTime();
|
const diff = Date.now() - new Date(dt).getTime();
|
||||||
const now = new Date();
|
const offset = new Date(diff);
|
||||||
const diff = now.getTime() - birthDateTimeStamp;
|
const years = offset.getUTCFullYear() - 1970;
|
||||||
|
const months = offset.getUTCMonth();
|
||||||
const ageDate = new Date(diff);
|
const days = offset.getUTCDate() - 1;
|
||||||
const years = ageDate.getUTCFullYear() - 1970;
|
|
||||||
const months = ageDate.getUTCMonth();
|
|
||||||
const days = ageDate.getUTCDate() - 1;
|
|
||||||
|
|
||||||
switch (only) {
|
switch (only) {
|
||||||
case 'year':
|
case 'year':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue