2018-10-10 07:22:06 +02:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-10-10 07:22:06 +02:00
|
|
|
import moment from 'moment';
|
2017-09-03 18:35:01 +02:00
|
|
|
|
2018-10-10 07:22:06 +02:00
|
|
|
type Props = {
|
2019-05-06 19:22:10 +02:00
|
|
|
date?: any,
|
2018-10-10 07:22:06 +02:00
|
|
|
timeAgo?: boolean,
|
|
|
|
formatOptions: {},
|
|
|
|
show?: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class DateTime extends React.PureComponent<Props> {
|
2017-12-21 22:08:54 +01:00
|
|
|
static SHOW_DATE = 'date';
|
|
|
|
static SHOW_TIME = 'time';
|
|
|
|
static SHOW_BOTH = 'both';
|
2017-09-18 02:52:57 +02:00
|
|
|
|
2017-09-03 18:35:01 +02:00
|
|
|
render() {
|
2019-12-05 07:09:08 +01:00
|
|
|
const { date, timeAgo } = this.props;
|
2017-09-18 02:52:57 +02:00
|
|
|
const show = this.props.show || DateTime.SHOW_BOTH;
|
2017-09-03 18:35:01 +02:00
|
|
|
|
2018-10-10 07:22:06 +02:00
|
|
|
if (timeAgo) {
|
2019-12-10 20:09:10 +01:00
|
|
|
if (!date) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moment is very liberal with it's rounding
|
|
|
|
// Wait to show "two years ago" until it's actually been two years (or higher)
|
2019-12-12 17:20:17 +01:00
|
|
|
const numberOfYearsSincePublish = Math.floor(moment().diff(date, 'years'));
|
2019-12-10 20:09:10 +01:00
|
|
|
|
|
|
|
if (numberOfYearsSincePublish === 1) {
|
|
|
|
return <span>{__('%numberOfYearsSincePublish% year ago', { numberOfYearsSincePublish })}</span>;
|
|
|
|
} else if (numberOfYearsSincePublish > 1) {
|
|
|
|
return <span>{__('%numberOfYearsSincePublish% years ago', { numberOfYearsSincePublish })}</span>;
|
|
|
|
}
|
|
|
|
|
2019-12-12 17:20:17 +01:00
|
|
|
const numberOfMonthsSincePublish = Math.floor(moment().diff(date, 'months'));
|
|
|
|
if (numberOfMonthsSincePublish === 1) {
|
|
|
|
return <span>{__('%numberOfMonthsSincePublish% month ago', { numberOfMonthsSincePublish })}</span>;
|
|
|
|
} else if (numberOfMonthsSincePublish > 1) {
|
|
|
|
return <span>{__('%numberOfMonthsSincePublish% months ago', { numberOfMonthsSincePublish })}</span>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const numberOfDaysSincePublish = Math.floor(moment().diff(date, 'days'));
|
|
|
|
if (numberOfDaysSincePublish === 1) {
|
|
|
|
return <span>{__('%numberOfDaysSincePublish% day ago', { numberOfDaysSincePublish })}</span>;
|
|
|
|
} else if (numberOfDaysSincePublish > 1) {
|
|
|
|
return <span>{__('%numberOfDaysSincePublish% days ago', { numberOfDaysSincePublish })}</span>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "just now", "a few minutes ago"
|
2019-12-10 20:09:10 +01:00
|
|
|
return <span>{moment(date).from(moment())}</span>;
|
2018-10-10 07:22:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 02:52:57 +02:00
|
|
|
return (
|
|
|
|
<span>
|
2019-12-05 07:09:08 +01:00
|
|
|
{date && (show === DateTime.SHOW_BOTH || show === DateTime.SHOW_DATE) && moment(date).format('MMMM Do, YYYY')}
|
2018-10-10 07:22:06 +02:00
|
|
|
{show === DateTime.SHOW_BOTH && ' '}
|
2019-05-07 23:38:29 +02:00
|
|
|
{date && (show === DateTime.SHOW_BOTH || show === DateTime.SHOW_TIME) && date.toLocaleTimeString()}
|
2017-12-21 22:08:54 +01:00
|
|
|
{!date && '...'}
|
2017-09-18 02:52:57 +02:00
|
|
|
</span>
|
|
|
|
);
|
2017-09-03 18:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DateTime;
|