UNPKG

@nacelle/compatibility-connector

Version:

Connect @nacelle/client-js-sdk to Nacelle's v2 back end with minimal code changes

22 lines (18 loc) 603 B
/** * @param input A date string * @returns a UNIX timestamp, or the original `input` if a UNIX timestamp could not be created from the `input`. */ export function toUnixTimestamp(input: string | unknown): number | unknown { if (typeof input !== 'string') { return input; } const jsTime = new Date(input).getTime(); if (Number.isNaN(jsTime)) { // Input could not be converted to a UNIX timestamp. // // NOTE: we can expect `jsTime` to be `NaN` when provided // any invalid string `input` (e.g. 'not-a-date'). return input; } return Math.floor(jsTime / 1000); }