Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
feat: use moment.js for timeAgo (#521)
Browse files Browse the repository at this point in the history
Also export the moment class to user space
this will enable the user to do
```
moment.locale('de');
```
in their `config.js`.
  • Loading branch information
akloeckner authored Nov 20, 2020
1 parent 73d1da9 commit 5227be4
Showing 1 changed file with 3 additions and 47 deletions.
50 changes: 3 additions & 47 deletions scripts/globals/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import angular from 'angular';
import moment from 'moment';

export const mergeObjects = angular.merge;

Expand Down Expand Up @@ -54,53 +55,8 @@ export const playSound = function (sound) {
};

export const timeAgo = function (time) {
time = +new Date(time);

const timeFormats = [
[60, 'seconds', 1],
[120, '1 minute ago', '1 minute from now'],
[3600, 'minutes', 60],
[7200, '1 hour ago', '1 hour from now'],
[86400, 'hours', 3600],
[172800, 'a day ago', 'Tomorrow'],
[604800, 'days', 86400],
[1209600, 'Last week', 'Next week'],
[2419200, 'weeks', 604800],
[4838400, 'a month ago', 'Next month'],
[29030400, 'months', 2419200],
[58060800, 'a year ago', 'Next year'],
[2903040000, 'years', 29030400],
];

let seconds = (+new Date() - time) / 1000;
let token = 'ago';
let listChoice = 1;


if (seconds < 0) {
seconds = Math.abs(seconds);
token = 'from now';
listChoice = 2;
}

if (seconds >= 0 && seconds < 5) {
return 'just now';
}

let i = 0;
let format;

while ((format = timeFormats[i++])) {
if (seconds < format[0]) {
if (typeof format[2] === 'string') {
return format[listChoice];
} else {
return Math.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token;
}
}
}

return time;
const momentInTime = moment(new Date(time));
return momentInTime.fromNow();
};

export const debounce = function (func, wait, immediate) {
Expand Down

0 comments on commit 5227be4

Please sign in to comment.