Created
June 26, 2019 19:18
-
-
Save codeBelt/88b05fd9162dc027dc5b7c31a2ac42cc to your computer and use it in GitHub Desktop.
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
export default class TimeUtility { | |
public static SECONDS_IN = { | |
WEEK: 604800, | |
DAY: 86400, | |
HOUR: 3600, | |
MINUTE: 60, | |
}; | |
public static getCountdownFromSeconds(seconds: number): string { | |
if (seconds < 60) { | |
return `${seconds}s`; | |
} | |
let time; | |
let mod; | |
if (seconds >= this.SECONDS_IN.WEEK) { | |
time = `${Math.floor(seconds / this.SECONDS_IN.WEEK)}w`; | |
mod = seconds % this.SECONDS_IN.WEEK; | |
} else if (seconds >= this.SECONDS_IN.DAY) { | |
time = `${Math.floor(seconds / this.SECONDS_IN.DAY)}d`; | |
mod = seconds % this.SECONDS_IN.DAY; | |
} else if (seconds >= this.SECONDS_IN.HOUR) { | |
time = `${Math.floor(seconds / this.SECONDS_IN.HOUR)}h`; | |
mod = seconds % this.SECONDS_IN.HOUR; | |
} else if (seconds >= this.SECONDS_IN.MINUTE) { | |
time = `${Math.floor(seconds / this.SECONDS_IN.MINUTE)}m`; | |
mod = seconds % this.SECONDS_IN.MINUTE; | |
} | |
if (mod > 0) { | |
return `${time} ${this.getCountdownFromSeconds(mod)}`; | |
} else { | |
return time; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment