blob: 7428b1ac7c32c2945aad6b5d64db8ca8c31860dd (
plain)
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
|
import moment from 'moment';
//A util that calculates the difference between a given time and current time
//Returns the difference in the largest possible unit of time (days > hours > minutes > seconds)
export const getTimePosted = (date_time: string) => {
const datePosted = moment(date_time);
const now = moment();
var time = date_time;
var difference = now.diff(datePosted, 'seconds');
//Creating elapsedTime string to display to user
// 0 to less than 1 minute
if (difference < 60) {
time = difference + ' seconds';
}
// 1 minute to less than 1 hour
else if (difference >= 60 && difference < 60 * 60) {
difference = now.diff(datePosted, 'minutes');
time = difference + (difference === 1 ? ' minute' : ' minutes');
}
//1 hour to less than 1 day
else if (difference >= 60 * 60 && difference < 24 * 60 * 60) {
difference = now.diff(datePosted, 'hours');
time = difference + (difference === 1 ? ' hour' : ' hours');
}
//Any number of days
else if (difference >= 24 * 60 * 60) {
difference = now.diff(datePosted, 'days');
time = difference + (difference === 1 ? ' day' : ' days');
}
return time;
};
|