Skip to content

Commit

Permalink
Merge pull request #2075 from npsedhain/npsedhain-formatDateTime
Browse files Browse the repository at this point in the history
Implement library to format time/dates for international users.
  • Loading branch information
Joel Bettner authored Mar 26, 2021
2 parents b0ae6a6 + fb91d05 commit ee11bcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 53 deletions.
71 changes: 19 additions & 52 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import moment from 'moment';
import 'moment-timezone';
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';

Expand All @@ -15,13 +14,15 @@ Onyx.connect({
* 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(timestamp) {
function getLocalMomentFromTimestamp(locale, timestamp) {
moment.locale(locale);
return moment.unix(timestamp).tz(timezone);
}

Expand All @@ -33,32 +34,24 @@ function getLocalMomentFromTimestamp(timestamp) {
* 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(timestamp, includeTimeZone = false) {
const date = getLocalMomentFromTimestamp(timestamp);
const isThisYear = moment().format('YYYY') === date.format('YYYY');
const isToday = moment().format('D MMM YYYY') === date.format('D MMM YYYY');
const yesterday = moment().add(-1, 'day').format('D MMM YYYY');
const isYesterday = yesterday === date.format('D MMM YYYY');
function timestampToDateTime(locale, timestamp, includeTimeZone = false) {
const date = getLocalMomentFromTimestamp(locale, timestamp);
const tz = includeTimeZone ? ' [UTC]Z' : '';

let format = 'LT';
if (isYesterday) {
format = `[Yesterday at] ${format}`;
} else if (!isToday) {
format = `MMM D [at] ${format}`;
} else if (!isThisYear) {
format = `MMM D, YYYY [at] ${format}`;
}

if (includeTimeZone) {
format = `${format} [UTC]Z`;
}

return date.format(format);
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}`,
});
}

/**
Expand All @@ -74,41 +67,15 @@ function timestampToDateTime(timestamp, includeTimeZone = false) {
* Jan 20 within the past year
* Jan 20, 2019 anything over 1 year
*
* @param {String} locale
* @param {Number} timestamp
*
* @returns {String}
*/
function timestampToRelative(timestamp) {
const date = getLocalMomentFromTimestamp(timestamp);
const durationFromLocalNow = moment.duration(
date.diff(getLocalMomentFromTimestamp(moment().unix())),
);
const round = num => Math.floor(Math.abs(num));

if (date.isAfter(moment().subtract(60, 'seconds'))) {
return '< 1 minute ago';
}

if (date.isAfter(moment().subtract(60, 'minutes'))) {
const minutes = round(durationFromLocalNow.asMinutes());
return `${minutes} ${Str.pluralize('minute', 'minutes', minutes)} ago`;
}

if (date.isAfter(moment().subtract(24, 'hours'))) {
const hours = round(durationFromLocalNow.asHours());
return `${hours} ${Str.pluralize('hour', 'hours', hours)} ago`;
}

if (date.isAfter(moment().subtract(30, 'days'))) {
const days = round(durationFromLocalNow.asDays());
return `${days} ${Str.pluralize('day', 'days', days)} ago`;
}

if (date.isAfter(moment().subtract(1, 'year'))) {
return date.format('MMM D');
}
function timestampToRelative(locale, timestamp) {
const date = getLocalMomentFromTimestamp(locale, timestamp);

return date.format('MMM D, YYYY');
return moment(date).fromNow();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItemDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const propTypes = {

const ReportActionItemDate = props => (
<Text style={[styles.chatItemMessageHeaderTimestamp]}>
{DateUtils.timestampToDateTime(props.timestamp)}
{DateUtils.timestampToDateTime('en', props.timestamp)}
</Text>
);

Expand Down

0 comments on commit ee11bcb

Please sign in to comment.