Files
Environment-Monitoring-System/ems-frontend/ems-monitoring-system/src/utils/formatter.ts
ChuXun 02a830145e 1
2025-10-25 19:18:43 +08:00

21 lines
633 B
TypeScript

import { format } from 'date-fns';
/**
* Formats a date-time string or Date object into a standardized "yyyy-MM-dd HH:mm:ss" format.
* If the input is invalid or null, it returns an empty string.
*
* @param dateTime The date-time to format (string, Date, or null/undefined).
* @returns The formatted date-time string or an empty string.
*/
export function formatDateTime(dateTime: string | Date | null | undefined): string {
if (!dateTime) {
return '';
}
try {
return format(new Date(dateTime), 'yyyy-MM-dd HH:mm:ss');
} catch (error) {
console.error('Error formatting date:', error);
return '';
}
}