-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added option to output duration and time
- Loading branch information
1 parent
763de08
commit db3eaa8
Showing
3 changed files
with
54 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {injectable} from "tsyringe" | ||
@injectable() | ||
export class DateUtil { | ||
formatDuration(milliseconds: number) { | ||
if(milliseconds === 0) { | ||
return "0ms"; | ||
} | ||
|
||
const seconds = Math.floor(milliseconds / 1000); | ||
const minutes = Math.floor(seconds / 60); | ||
const hours = Math.floor(minutes / 60); | ||
const days = Math.floor(hours / 24); | ||
const years = Math.floor(days / 365); | ||
|
||
const parts = []; | ||
if (years > 0) parts.push(`${years} year${years > 1 ? 's' : ''}`); | ||
if (days > 0) parts.push(`${days} day${days > 1 ? 's' : ''}`); | ||
if (hours > 0) parts.push(`${hours} hour${hours > 1 ? 's' : ''}`); | ||
if (minutes > 0) parts.push(`${minutes} minute${minutes > 1 ? 's' : ''}`); | ||
parts.push(`${seconds % 60} second${seconds % 60 > 1 ? 's' : ''}`); | ||
|
||
return parts.join(", ").replace(/, (.*)$/, " and $1"); | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from "./date.util"; | ||
export * from "./metadata.util"; | ||
export * from "./request.util"; |