(cherry picked from commit 05edd44ed6dfe73c25da021ef2600e609852f7e0) (cherry picked from commit ca372bee258523339aff2b868f8f9a619d44dbca) (cherry picked from commit 2a662afaef0bed9f8cd153d60eee5ecbe85508ba) Closes #10039 Closes #10045 Closes #10046pull/10195/head
parent
8d189523c4
commit
6e81d5917e
@ -1,43 +0,0 @@
|
||||
import moment from 'moment';
|
||||
import formatTime from 'Utilities/Date/formatTime';
|
||||
import isInNextWeek from 'Utilities/Date/isInNextWeek';
|
||||
import isToday from 'Utilities/Date/isToday';
|
||||
import isTomorrow from 'Utilities/Date/isTomorrow';
|
||||
import isYesterday from 'Utilities/Date/isYesterday';
|
||||
import translate from 'Utilities/String/translate';
|
||||
|
||||
function getRelativeDate(date, shortDateFormat, showRelativeDates, { timeFormat, includeSeconds = false, timeForToday = false } = {}) {
|
||||
if (!date) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const isTodayDate = isToday(date);
|
||||
|
||||
if (isTodayDate && timeForToday && timeFormat) {
|
||||
return formatTime(date, timeFormat, { includeMinuteZero: true, includeSeconds });
|
||||
}
|
||||
|
||||
if (!showRelativeDates) {
|
||||
return moment(date).format(shortDateFormat);
|
||||
}
|
||||
|
||||
if (isYesterday(date)) {
|
||||
return translate('Yesterday');
|
||||
}
|
||||
|
||||
if (isTodayDate) {
|
||||
return translate('Today');
|
||||
}
|
||||
|
||||
if (isTomorrow(date)) {
|
||||
return translate('Tomorrow');
|
||||
}
|
||||
|
||||
if (isInNextWeek(date)) {
|
||||
return moment(date).format('dddd');
|
||||
}
|
||||
|
||||
return moment(date).format(shortDateFormat);
|
||||
}
|
||||
|
||||
export default getRelativeDate;
|
@ -0,0 +1,82 @@
|
||||
import moment from 'moment';
|
||||
import formatTime from 'Utilities/Date/formatTime';
|
||||
import isInNextWeek from 'Utilities/Date/isInNextWeek';
|
||||
import isToday from 'Utilities/Date/isToday';
|
||||
import isTomorrow from 'Utilities/Date/isTomorrow';
|
||||
import isYesterday from 'Utilities/Date/isYesterday';
|
||||
import translate from 'Utilities/String/translate';
|
||||
import formatDateTime from './formatDateTime';
|
||||
|
||||
interface GetRelativeDateOptions {
|
||||
date?: string;
|
||||
shortDateFormat: string;
|
||||
showRelativeDates: boolean;
|
||||
timeFormat?: string;
|
||||
includeSeconds?: boolean;
|
||||
timeForToday?: boolean;
|
||||
includeTime?: boolean;
|
||||
}
|
||||
|
||||
function getRelativeDate({
|
||||
date,
|
||||
shortDateFormat,
|
||||
showRelativeDates,
|
||||
timeFormat,
|
||||
includeSeconds = false,
|
||||
timeForToday = false,
|
||||
includeTime = false,
|
||||
}: GetRelativeDateOptions) {
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ((includeTime || timeForToday) && !timeFormat) {
|
||||
throw new Error(
|
||||
"getRelativeDate: 'timeFormat' is required when 'includeTime' or 'timeForToday' is true"
|
||||
);
|
||||
}
|
||||
|
||||
const isTodayDate = isToday(date);
|
||||
const time = timeFormat
|
||||
? formatTime(date, timeFormat, {
|
||||
includeMinuteZero: true,
|
||||
includeSeconds,
|
||||
})
|
||||
: '';
|
||||
|
||||
if (isTodayDate && timeForToday) {
|
||||
return time;
|
||||
}
|
||||
|
||||
if (!showRelativeDates) {
|
||||
return moment(date).format(shortDateFormat);
|
||||
}
|
||||
|
||||
if (isYesterday(date)) {
|
||||
return includeTime
|
||||
? translate('YesterdayAt', { time })
|
||||
: translate('Yesterday');
|
||||
}
|
||||
|
||||
if (isTodayDate) {
|
||||
return includeTime ? translate('TodayAt', { time }) : translate('Today');
|
||||
}
|
||||
|
||||
if (isTomorrow(date)) {
|
||||
return includeTime
|
||||
? translate('TomorrowAt', { time })
|
||||
: translate('Tomorrow');
|
||||
}
|
||||
|
||||
if (isInNextWeek(date)) {
|
||||
const day = moment(date).format('dddd');
|
||||
|
||||
return includeTime ? translate('DayOfWeekAt', { day, time }) : day;
|
||||
}
|
||||
|
||||
return includeTime
|
||||
? formatDateTime(date, shortDateFormat, timeFormat, { includeSeconds })
|
||||
: moment(date).format(shortDateFormat);
|
||||
}
|
||||
|
||||
export default getRelativeDate;
|
Loading…
Reference in new issue