-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
DateUtils.js
99 lines (90 loc) · 2.58 KB
/
DateUtils.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
import moment from 'moment';
import 'moment-timezone';
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
let timezone;
Onyx.connect({
key: ONYXKEYS.MY_PERSONAL_DETAILS,
callback: (val) => {
timezone = val ? val.timezone : CONST.DEFAULT_TIME_ZONE.selected;
// Make sure that if we have a timezone in object format that we're getting the selected timezone name
// Older timezone formats only include the timezone name, but the newer format also included whether or
// not the timezone was selected automatically
if (_.isObject(timezone)) {
timezone = val.timezone.selected;
}
},
});
/**
* Gets the user's stored time-zone NVP and returns a localized
* Moment object for the given timestamp
*
* @param {String} locale
* @param {Number} timestamp
*
* @returns {Moment}
*
* @private
*/
function getLocalMomentFromTimestamp(locale, timestamp) {
moment.locale(locale);
return moment.unix(timestamp).tz(timezone);
}
/**
* Formats a timestamp to local date and time string
*
* e.g.
*
* Jan 20 at 5:30 PM within the past year
* Jan 20, 2019 at 5:30 PM anything over 1 year ago
*
* @param {String} locale
* @param {Number} timestamp
* @param {Boolean} includeTimeZone
*
* @returns {String}
*/
function timestampToDateTime(locale, timestamp, includeTimeZone = false) {
const date = getLocalMomentFromTimestamp(locale, timestamp);
const tz = includeTimeZone ? ' [UTC]Z' : '';
return moment(date).calendar({
sameDay: `[Today at] LT${tz}`,
nextDay: `[Tomorrow at] LT${tz}`,
nextWeek: `MMM D [at] LT${tz}`,
lastDay: `[Yesterday at] LT${tz}`,
lastWeek: `MMM D [at] LT${tz}`,
sameElse: `MMM D, YYYY [at] LT${tz}`,
});
}
/**
* Converts a timestamp into a localized string representation
* that's relative to current moment in time.
*
* e.g.
*
* < 1 minute ago within the past minute
* 12 minutes ago within the past hour
* 1 hour ago within the past day
* 3 days ago within the past month
* Jan 20 within the past year
* Jan 20, 2019 anything over 1 year
*
* @param {String} locale
* @param {Number} timestamp
*
* @returns {String}
*/
function timestampToRelative(locale, timestamp) {
const date = getLocalMomentFromTimestamp(locale, timestamp);
return moment(date).fromNow();
}
/**
* @namespace DateUtils
*/
const DateUtils = {
timestampToRelative,
timestampToDateTime,
};
export default DateUtils;