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 sortElementsBySubmissionDate = (array) => {
|
---|
10 | return array.slice().sort((a, b) => {
|
---|
11 | return new Date(b).getTime() - new Date(a.postedOn).getTime()
|
---|
12 | });
|
---|
13 | }
|
---|
14 |
|
---|
15 | export const formatRelativeTime = (dateString) => {
|
---|
16 | const date = new Date(dateString);
|
---|
17 | const now = new Date();
|
---|
18 | const diffTime = now - date;
|
---|
19 |
|
---|
20 | // Define time intervals in milliseconds
|
---|
21 | const minute = 60 * 1000;
|
---|
22 | const hour = minute * 60;
|
---|
23 | const day = hour * 24;
|
---|
24 | const week = day * 7;
|
---|
25 | const month = day * 30;
|
---|
26 |
|
---|
27 | // Calculate the relative time
|
---|
28 | if (diffTime < minute) {
|
---|
29 | return 'just now';
|
---|
30 | } else if (diffTime < hour) {
|
---|
31 | const minutes = Math.floor(diffTime / minute);
|
---|
32 | return `${minutes} ${minutes === 1 ? 'min' : 'min'} ago`;
|
---|
33 | } else if (diffTime < day) {
|
---|
34 | const hours = Math.floor(diffTime / hour);
|
---|
35 | return `${hours} ${hours === 1 ? 'hour' : 'hours'} ago`;
|
---|
36 | } else if (diffTime < week) {
|
---|
37 | const days = Math.floor(diffTime / day);
|
---|
38 | return `${days} ${days === 1 ? 'day' : 'days'} ago`;
|
---|
39 | } else if (diffTime < month) {
|
---|
40 | const weeks = Math.floor(diffTime / week);
|
---|
41 | return `${weeks} ${weeks === 1 ? 'week' : 'weeks'} ago`;
|
---|
42 | } else {
|
---|
43 | const months = Math.floor(diffTime / month);
|
---|
44 | return `${months} ${months === 1 ? 'month' : 'months'} ago`;
|
---|
45 | }
|
---|
46 | } |
---|