Skip to content

Commit

Permalink
formatTime() should return full unit of time
Browse files Browse the repository at this point in the history
There is a fair difference between 7m (months) and 7m (minutes).
  • Loading branch information
ihavenoface committed Feb 2, 2022
1 parent 2028d9a commit 6623900
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/helpers/Time.js
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`;
}

0 comments on commit 6623900

Please sign in to comment.