Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Created June 26, 2019 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeBelt/88b05fd9162dc027dc5b7c31a2ac42cc to your computer and use it in GitHub Desktop.
Save codeBelt/88b05fd9162dc027dc5b7c31a2ac42cc to your computer and use it in GitHub Desktop.
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