Skip to main content

Function: safeParseTimestamp()

safeParseTimestamp(value: unknown, defaultValue?: number): number

Defined in: shared/utils/safeConversions.ts:304

Safely converts a value to a timestamp with fallback.

Parametersโ€‹

valueโ€‹

unknown

Value to convert to timestamp

defaultValue?โ€‹

number

Fallback value if conversion fails (default: current time)

Returnsโ€‹

number

Valid timestamp in milliseconds, or the default value

Remarksโ€‹

Validates that the timestamp is positive and not unreasonably far in the future (allows up to 1 day ahead to account for clock skew). Uses current time as default when no defaultValue is provided. Useful for validating timestamps from external sources.

Exampleโ€‹

safeParseTimestamp("1640995200000"); // Valid timestamp
safeParseTimestamp("0"); // Current time (invalid)
safeParseTimestamp("-1000"); // Current time (negative)
const future = Date.now() + 86400000 * 2; // 2 days ahead
safeParseTimestamp(future.toString()); // Current time (too far in future)