-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathdate.js
More file actions
41 lines (37 loc) · 1.04 KB
/
date.js
File metadata and controls
41 lines (37 loc) · 1.04 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
import moment from 'moment';
/**
* format
* - before a week ago from now: MM/DD/YY, h:mm meridiem
* - yesterday: Yesterday at h:mm
* - a week ago from now: DAY h:mm meridiem
* - today: h:mm meridiem
*
* @param {Object} time moment object for specific time
* @returns {String} nicely formatted timestamp
*/
export function formatDate(time) {
const now = moment();
let rawTime = time || moment();
switch (typeof rawTime) {
case 'number':
case 'string':
rawTime = moment(rawTime);
break;
default:
}
// note endOf(day) compare to normalize now no matter how it was created
if (now.endOf('day').diff(rawTime, 'days') === 0) {
// today
return rawTime.format('LT');
}
if (now.startOf('day').diff(rawTime) <= 86400000) {
// yesterday (60*60*24*1000 = 86400000)
return rawTime.calendar();
}
if (now.startOf('day').diff(rawTime) <= 518400000) {
// 6 days ago from today (60*60*24*6*1000 = 518400000)
return rawTime.format('dddd LT');
}
return rawTime.format('L, LT');
}
export default {};