1
This commit is contained in:
20
ems-frontend/ems-monitoring-system/src/utils/formatter.ts
Normal file
20
ems-frontend/ems-monitoring-system/src/utils/formatter.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
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 '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user