2024-08-15 09:17:36 +08:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
|
2026-01-23 09:33:34 +08:00
|
|
|
export function formatDate(date: any, format?: string) {
|
|
|
|
|
const thisFormat = format || 'DD/MM/YYYY HH:mm:ss';
|
2024-11-18 17:23:49 +08:00
|
|
|
if (!date) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2026-01-23 09:33:34 +08:00
|
|
|
return dayjs(date).format(thisFormat);
|
2024-11-18 17:23:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatTime(date: any) {
|
|
|
|
|
if (!date) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return dayjs(date).format('HH:mm:ss');
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
export function today() {
|
|
|
|
|
return formatDate(dayjs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function lastDay() {
|
|
|
|
|
return formatDate(dayjs().subtract(1, 'days'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function lastWeek() {
|
|
|
|
|
return formatDate(dayjs().subtract(1, 'weeks'));
|
|
|
|
|
}
|
2025-02-10 17:38:10 +08:00
|
|
|
|
|
|
|
|
export function formatPureDate(date: any) {
|
|
|
|
|
if (!date) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return dayjs(date).format('DD/MM/YYYY');
|
|
|
|
|
}
|
2025-07-30 09:48:51 +08:00
|
|
|
|
|
|
|
|
export function formatStandardDate(date: any) {
|
|
|
|
|
if (!date) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
const parsedDate = dayjs(date);
|
|
|
|
|
if (!parsedDate.isValid()) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return parsedDate.format('YYYY-MM-DD');
|
|
|
|
|
}
|
2025-10-09 12:36:19 +08:00
|
|
|
|
|
|
|
|
export function formatSecondsToHumanReadable(seconds: number): string {
|
|
|
|
|
if (isNaN(seconds) || seconds < 0) {
|
|
|
|
|
return '0s';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const h = Math.floor(seconds / 3600);
|
|
|
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
|
|
|
// const s = toFixed(seconds % 60, 3);
|
|
|
|
|
const s = seconds % 60;
|
|
|
|
|
const formattedSeconds = s === 0 ? '0' : s.toFixed(3).replace(/\.?0+$/, '');
|
|
|
|
|
const parts = [];
|
|
|
|
|
if (h > 0) parts.push(`${h}h `);
|
|
|
|
|
if (m > 0) parts.push(`${m}m `);
|
|
|
|
|
if (s || parts.length === 0) parts.push(`${formattedSeconds}s`);
|
|
|
|
|
|
|
|
|
|
return parts.join('');
|
|
|
|
|
}
|