snp-batch-validation/frontend/src/utils/formatters.ts

59 lines
1.9 KiB
TypeScript
Raw Normal View 히스토리

export function formatDateTime(dateTimeStr: string | null | undefined): string {
if (!dateTimeStr) return '-';
try {
const date = new Date(dateTimeStr);
if (isNaN(date.getTime())) return '-';
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
const h = String(date.getHours()).padStart(2, '0');
const min = String(date.getMinutes()).padStart(2, '0');
const s = String(date.getSeconds()).padStart(2, '0');
return `${y}-${m}-${d} ${h}:${min}:${s}`;
} catch {
return '-';
}
}
export function formatDateTimeShort(dateTimeStr: string | null | undefined): string {
if (!dateTimeStr) return '-';
try {
const date = new Date(dateTimeStr);
if (isNaN(date.getTime())) return '-';
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
const h = String(date.getHours()).padStart(2, '0');
const min = String(date.getMinutes()).padStart(2, '0');
return `${m}/${d} ${h}:${min}`;
} catch {
return '-';
}
}
export function formatDuration(ms: number | null | undefined): string {
if (ms == null || ms < 0) return '-';
const totalSeconds = Math.floor(ms / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
if (hours > 0) return `${hours}시간 ${minutes}${seconds}`;
if (minutes > 0) return `${minutes}${seconds}`;
return `${seconds}`;
}
export function calculateDuration(
startTime: string | null | undefined,
endTime: string | null | undefined,
): string {
if (!startTime) return '-';
const start = new Date(startTime).getTime();
if (isNaN(start)) return '-';
if (!endTime) return '실행 중...';
const end = new Date(endTime).getTime();
if (isNaN(end)) return '-';
return formatDuration(end - start);
}