Add function to get time ago
This commit is contained in:
parent
629977e351
commit
6daa4493e2
1 changed files with 25 additions and 0 deletions
25
app/helpers/time-utils.js
Normal file
25
app/helpers/time-utils.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
export function timeSince(date) {
|
||||
const seconds = Math.floor((new Date() - date) / 1000);
|
||||
|
||||
let interval = Math.floor(seconds / 31536000);
|
||||
if (interval > 1) {
|
||||
return interval + ' years';
|
||||
}
|
||||
interval = Math.floor(seconds / 2592000);
|
||||
if (interval > 1) {
|
||||
return interval + ' months';
|
||||
}
|
||||
interval = Math.floor(seconds / 86400);
|
||||
if (interval > 1) {
|
||||
return interval + ' days';
|
||||
}
|
||||
interval = Math.floor(seconds / 3600);
|
||||
if (interval > 1) {
|
||||
return interval + ' hours';
|
||||
}
|
||||
interval = Math.floor(seconds / 60);
|
||||
if (interval > 1) {
|
||||
return interval + ' minutes';
|
||||
}
|
||||
return Math.floor(seconds) + ' seconds';
|
||||
}
|
Reference in a new issue