Skip to content

Commit

Permalink
Merge pull request #39 from squidit/formatTime-function
Browse files Browse the repository at this point in the history
Add formatTime function
  • Loading branch information
JoaoBianco committed Dec 13, 2023
2 parents a2fa36b + 9215d94 commit 3423b31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/helpers/formatTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Format time in seconds to a readable format.
* @param {number} totalSeconds - Time in seconds
* @returns {string} A string that contains the formatted time
*
* @example
* // returns '1d 01h 01m 01s'
* formatTime(90061)
*/

export function formatTime(totalSeconds: number): string {
const seconds = totalSeconds % 60
const minutes = Math.floor(totalSeconds / 60) % 60
const hours = Math.floor(totalSeconds / 3600) % 24
const days = Math.floor(totalSeconds / 86400)

if (days > 0) {
return `${days}d ${hours.toString().padStart(2, '0')}h ${minutes
.toString()
.padStart(2, '0')}m ${seconds.toString().padStart(2, '0')}s`
} else if (hours > 0) {
return `${hours.toString().padStart(2, '0')}h ${minutes
.toString()
.padStart(2, '0')}m ${seconds.toString().padStart(2, '0')}s`
} else if (minutes > 0) {
return `${minutes.toString().padStart(2, '0')}m ${seconds
.toString()
.padStart(2, '0')}s`
} else {
return `${seconds.toString().padStart(2, '0')}s`
}
}
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@squidit/ngx-css",
"version": "1.2.2",
"version": "1.2.21",
"peerDependencies": {
"@angular/common": ">=15.0.0",
"@angular/core": ">=15.0.0",
Expand Down

0 comments on commit 3423b31

Please sign in to comment.