-
Notifications
You must be signed in to change notification settings - Fork 51
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
1 parent
a34850a
commit fd9f444
Showing
3 changed files
with
35 additions
and
16 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,33 @@ | ||
export const formatDate = (dateToCheckISO) => { | ||
// Calculate the date difference between the current date and the date to check | ||
// and format it like: 2y, 3mo, 4d, 5h, 6m (no seconds because updates will be too frequent) | ||
|
||
const d1 = new Date(), | ||
t1 = d1.getTime(); | ||
const d2 = new Date(dateToCheckISO), | ||
t2 = d2.getTime(); | ||
const diff = t1 - t2; | ||
const seconds = Math.floor(diff / 1000); | ||
const minutes = Math.floor(seconds / 60); | ||
const hours = Math.floor(minutes / 60); | ||
const days = Math.floor(hours / 24); | ||
const months = Math.floor(days / 30); | ||
const years = Math.floor(months / 12); | ||
|
||
let formatted = "now"; | ||
// we only want one unit of time, and it should be the largest unit | ||
for (const [unit, value] of [ | ||
["y", years], | ||
["mo", months], | ||
["d", days], | ||
["h", hours], | ||
["m", minutes], | ||
]) { | ||
if (value > 0) { | ||
formatted = `${value}${unit}`; | ||
break; | ||
} | ||
} | ||
|
||
return formatted; | ||
}; |
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