-
-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
6 additions
and
15 deletions.
There are no files selected for viewing
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
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; | ||
} |