Safely parses different timestamp formats into a standardized Date object
Handles multiple timestamp formats that may exist in the dataset:
Returns null for invalid or empty timestamps to enable safe optional chaining.
export const parseTimestamp = (timestamp: string): Date | null => { if (!timestamp) return null; try { if (!isNaN(Number(timestamp))) { return new Date(Number(timestamp)); } else { return new Date(timestamp); } } catch (error) { console.error("Error parsing timestamp:", error); return null; }}; Copy
export const parseTimestamp = (timestamp: string): Date | null => { if (!timestamp) return null; try { if (!isNaN(Number(timestamp))) { return new Date(Number(timestamp)); } else { return new Date(timestamp); } } catch (error) { console.error("Error parsing timestamp:", error); return null; }};
Timestamp string in any supported format
Standardized Date object or null if invalid
Safely parses different timestamp formats into a standardized Date object
Handles multiple timestamp formats that may exist in the dataset:
Returns null for invalid or empty timestamps to enable safe optional chaining.
Source