Skip to content

Commit

Permalink
refactor webapp/utils/time
Browse files Browse the repository at this point in the history
  • Loading branch information
nkappler committed Jan 13, 2024
1 parent 69cf63e commit 6166ecd
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions webapp/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
export const timestampToString = (value: number, includeDays = false): string => {
const days = Math.floor(value / (24 * 60 * 60));
const secAfterDays = value - days * (24 * 60 * 60);
const hours = Math.floor(secAfterDays / (60 * 60));
const secAfterHours = secAfterDays - hours * (60 * 60);
const minutes = Math.floor(secAfterHours / 60);
const seconds = secAfterHours - minutes * 60;
export const timestampToString = (timestampSeconds: number, includeDays = false): string => {
const timeString = new Date(timestampSeconds * 1000).toLocaleTimeString([], { timeZone: "UTC" });
if (!includeDays) return timeString;

const dHours = hours > 9 ? hours : "0" + hours;
const dMins = minutes > 9 ? minutes : "0" + minutes;
const dSecs = seconds > 9 ? seconds : "0" + seconds;

if (includeDays) {
return days + " days " + dHours + ":" + dMins + ":" + dSecs;
}
return dHours + ":" + dMins + ":" + dSecs;
const secondsPerDay = 60 * 60 * 24;
const days = Math.floor(timestampSeconds / secondsPerDay);
return new Intl.RelativeTimeFormat().format(-days, "day") + " " + timeString;
}

0 comments on commit 6166ecd

Please sign in to comment.