Skip to content

Instantly share code, notes, and snippets.

@ilomon10
Last active November 11, 2020 13:30
Show Gist options
  • Save ilomon10/e05893b911dd2254f2d783c7b2a56dac to your computer and use it in GitHub Desktop.
Save ilomon10/e05893b911dd2254f2d783c7b2a56dac to your computer and use it in GitHub Desktop.
Parse any format of time
// Read:
// https://stackoverflow.com/a/12423012/8271526
// https://stackoverflow.com/a/9186301/8271526
function timestampParse(time: string | number): number {
let t = 0;
if (typeof time === "number")
if (/^[0-9]{10}$/.test(`${time}`)) // is unix
t = time * 1000;
else
t = time;
const result = new Date(t).getTime();
if (result > 0) // is valid
return result;
else return new Date().getTime();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment