21 lines
633 B
TypeScript
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 '';
|
|
}
|
|
}
|