-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
formatTime() should return full unit of time
There is a fair difference between 7m (months) and 7m (minutes).
- Loading branch information
1 parent
2028d9a
commit 6623900
Showing
1 changed file
with
12 additions
and
22 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,25 +1,15 @@ | ||
var periods = { | ||
month: 30 * 24 * 60 * 60 * 1000, | ||
week: 7 * 24 * 60 * 60 * 1000, | ||
day: 24 * 60 * 60 * 1000, | ||
hour: 60 * 60 * 1000, | ||
minute: 60 * 1000 | ||
}; | ||
var periods = [ | ||
{amount: 30 * 24 * 60 * 60 * 1000, str: "month"}, | ||
{amount: 7 * 24 * 60 * 60 * 1000, str: "week"}, | ||
{amount: 24 * 60 * 60 * 1000, str: "day"}, | ||
{amount: 60 * 60 * 1000, str: "hour"}, | ||
{amount: 60 * 1000, str: "minute"}, | ||
]; | ||
|
||
export function formatTime(timeCreated) { | ||
var diff = Date.now() - new Date(timeCreated); | ||
|
||
if (diff > periods.month) { | ||
return Math.floor(diff / periods.month) + "m ago"; | ||
} else if (diff > periods.week) { | ||
return Math.floor(diff / periods.week) + "w ago"; | ||
} else if (diff > periods.day) { | ||
return Math.floor(diff / periods.day) + "d ago"; | ||
} else if (diff > periods.hour) { | ||
return Math.floor(diff / periods.hour) + "h ago"; | ||
} else if (diff > periods.minute) { | ||
return Math.floor(diff / periods.minute) + "m ago"; | ||
} | ||
|
||
return "Just now"; | ||
const diff = Date.now() - new Date(timeCreated); | ||
const period = periods.find(({amount}) => diff > amount); | ||
if (!period) return 'Just now'; | ||
const count = Math.floor(diff / period.amount); | ||
return `${count} ${period.str}${count > 1 ? 's' : ''} ago`; | ||
} |