-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (99 loc) · 3.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const moment = require('moment')
/**
* Formats a timestamp as a human-readable string
* @param {string} timestamp - The timestamp to format
* @returns {string} A human-readable string indicating how long ago the timestamp was (e.g. "a few seconds ago", "5 minutes ago", "an hour ago", "3 days ago", etc.)
* @throws {Error} If the timestamp is invalid or missing
*/
function humanTime(timestamp) {
try {
if (!timestamp || !moment(timestamp).isValid()) {
throw new Error('Invalid timestamp');
}
const now = moment();
const time = moment(timestamp);
const duration = moment.duration(now.diff(time));
const years = duration.years();
const months = duration.months();
const days = duration.days();
const hours = duration.hours();
const minutes = duration.minutes();
const seconds = duration.seconds();
let elapsed = '';
if (years > 0) {
elapsed = `${years} ${years === 1 ? 'year' : 'years'}`;
} else if (months > 0) {
elapsed = `${months} ${months === 1 ? 'month' : 'months'}`;
} else if (days > 0) {
elapsed = `${days} ${days === 1 ? 'day' : 'days'}`;
} else if (hours > 0) {
elapsed = `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
} else if (minutes > 0) {
elapsed = `${minutes} ${minutes === 1 ? 'minute' : 'minutes'}`;
} else {
elapsed = `${seconds} ${seconds === 1 ? 'second' : 'seconds'}`;
}
return elapsed;
} catch (error) {
console.error(error);
return 'Invalid timestamp';
}
}
function relativeDate(publishedDate) {
if (!publishedDate) {
throw new Error('Published date is required');
}
const now = moment();
if (!moment(publishedDate).isValid()) {
throw new Error('Invalid published date');
}
const diff = now.diff(publishedDate, 'days');
let formattedDate;
if (diff < 1) {
formattedDate = moment(publishedDate).fromNow();
} else if (diff < 7) {
formattedDate = moment(publishedDate).format('dddd');
} else if (diff < 365) {
formattedDate = moment(publishedDate).format('MMMM D');
} else {
formattedDate = moment(publishedDate).format('MMM D, YYYY');
}
return formattedDate.replace('in ', '');
}
function FutureDateCounter(futureDate, options={}) {
if (!futureDate) {
throw new Error('Future date is required');
}
if (options.start_date) {
const startMoment = moment(options.start_date);
if (!startMoment.isValid()) {
throw new Error('Invalid start date');
}
}
const now = moment(options.start_date || undefined);
const diff = moment(futureDate).diff(now);
if (diff < 0) {
throw new Error('Future date must be in the future');
}
const duration = moment.duration(diff);
const years = Math.floor(duration.asYears());
const months = Math.floor(duration.asMonths()) % 12;
const days = duration.days();
const hours = duration.hours();
const minutes = duration.minutes();
const seconds = duration.seconds();
if (years > 0) {
return `${years} year${years > 1 ? 's' : ''}`;
} else if (months > 0) {
return `${months} month${months > 1 ? 's' : ''}`;
} else if (days > 0) {
return `${days} day${days > 1 ? 's' : ''}`;
} else if (hours > 0) {
return `${hours} hour${hours > 1 ? 's' : ''}`;
} else if (minutes > 0) {
return `${minutes} minute${minutes > 1 ? 's' : ''}`;
} else {
return `${seconds} second${seconds > 1 ? 's' : ''}`;
}
}
module.exports = {humanTime ,relativeDate ,FutureDateCounter };