Last active
November 11, 2020 13:30
-
-
Save ilomon10/e05893b911dd2254f2d783c7b2a56dac to your computer and use it in GitHub Desktop.
Parse any format of time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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