-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathcalculateDateText.js
More file actions
58 lines (55 loc) · 1.46 KB
/
calculateDateText.js
File metadata and controls
58 lines (55 loc) · 1.46 KB
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
import React from 'react';
import moment from 'moment';
import {
FormattedDate,
FormattedMessage
} from 'react-intl';
export default function calculateDateText(fromDate, now, toDate) {
let text;
// toDate and fromDate aren't in the same year OR
// toDate is not in current year, then show year.
const sameYearMessages = toDate.diff(fromDate, 'years') === 0;
const sameYearNow = toDate.diff(now, 'years') === 0;
if (!sameYearMessages || !sameYearNow) {
text = (
<FormattedDate
day="numeric"
month="long"
value={toDate.toDate()}
year="numeric"
/>
);
}
// from.day < to.day assume from.day < now.day. must check to.day == now.day
else if (now.diff(toDate, 'days') === 0) {
text = (
<FormattedMessage
defaultMessage="Today"
description="Day indicator for the current day"
id="ciscospark.datetime.today"
/>
);
}
// from.day < to.day < now.day therefore from cannot be yesterday
// only need to check to.day == now.day - 1
else if (moment(now).subtract(1, 'days').diff(toDate, 'days') === 0) {
text = (
<FormattedMessage
defaultMessage="Yesterday"
description="Day indicator for the previous day"
id="ciscospark.datetime.yesterday"
/>
);
}
else {
// older than yesterday.
text = (
<FormattedDate
day="numeric"
month="long"
value={toDate.toDate()}
/>
);
}
return text;
}