Last active
March 25, 2022 14:33
-
-
Save hebertcisco/108d7b914e2b65ded06effa7ba31788c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type TypeGetTimeRemaining = { | |
total: number; | |
days: number; | |
hours: number; | |
minutes: number; | |
seconds: number; | |
}; | |
function getTimeRemaining(endtime: string): TypeGetTimeRemaining { | |
let time = Date.parse(endtime) - Date.parse(new Date().toDateString()); | |
const THOUSAND = 1000; | |
const HOURS_IN_DAY = 24; | |
const MINUTES_IN_HOUR = 60; | |
const SECONDS_IN_MINUTE = MINUTES_IN_HOUR; | |
let seconds = Math.floor(time / THOUSAND % SECONDS_IN_MINUTE); | |
let minutes = Math.floor( | |
time / THOUSAND / SECONDS_IN_MINUTE % MINUTES_IN_HOUR | |
); | |
let hours = Math.floor( | |
time / (THOUSAND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR) % HOURS_IN_DAY | |
); | |
let days = Math.floor( | |
time / (THOUSAND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY) | |
); | |
return { | |
total: time, | |
days: days, | |
hours: hours, | |
minutes: minutes, | |
seconds: seconds | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment