main
Last change
on this file since 4d97b63 was 08f82ec, checked in by 223021 <daniel.ilievski.2@…>, 5 months ago |
Did more refactoring
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 |
|
---|
2 |
|
---|
3 | export const sortElementsBy = (array, column) => {
|
---|
4 | return array.slice().sort((a, b) => {
|
---|
5 | return new Date(b[column]).getTime() - new Date(a[column]).getTime()
|
---|
6 | });
|
---|
7 | }
|
---|
8 |
|
---|
9 | export const formatRelativeTime = (dateString) => {
|
---|
10 | const date = new Date(dateString);
|
---|
11 | const now = new Date();
|
---|
12 | const diffTime = now - date;
|
---|
13 |
|
---|
14 | const minute = 60 * 1000;
|
---|
15 | const hour = minute * 60;
|
---|
16 | const day = hour * 24;
|
---|
17 | const week = day * 7;
|
---|
18 | const month = day * 30;
|
---|
19 |
|
---|
20 | if (diffTime < minute) {
|
---|
21 | return 'just now';
|
---|
22 | } else if (diffTime < hour) {
|
---|
23 | const minutes = Math.floor(diffTime / minute);
|
---|
24 | return `${minutes} ${minutes === 1 ? 'min' : 'min'} ago`;
|
---|
25 | } else if (diffTime < day) {
|
---|
26 | const hours = Math.floor(diffTime / hour);
|
---|
27 | return `${hours} ${hours === 1 ? 'hour' : 'hours'} ago`;
|
---|
28 | } else if (diffTime < week) {
|
---|
29 | const days = Math.floor(diffTime / day);
|
---|
30 | return `${days} ${days === 1 ? 'day' : 'days'} ago`;
|
---|
31 | } else if (diffTime < month) {
|
---|
32 | const weeks = Math.floor(diffTime / week);
|
---|
33 | return `${weeks} ${weeks === 1 ? 'week' : 'weeks'} ago`;
|
---|
34 | } else {
|
---|
35 | const months = Math.floor(diffTime / month);
|
---|
36 | return `${months} ${months === 1 ? 'month' : 'months'} ago`;
|
---|
37 | }
|
---|
38 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.