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';
|
2017-09-18 02:52:57 +02:00
|
|
|
|
2020-05-11 09:57:03 +02:00
|
|
|
static getTimeAgoStr(date: any) {
|
|
|
|
const suffixList = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', ''];
|
|
|
|
var duration = 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < suffixList.length; ++i) {
|
|
|
|
// moment() is very liberal with it's rounding.
|
|
|
|
// Always round down dates for better youtube parity.
|
|
|
|
duration = Math.floor(moment().diff(date, suffixList[i]));
|
|
|
|
if (duration > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i === suffixList.length) {
|
|
|
|
// This should never happen since we are handling up to 'seconds' now,
|
|
|
|
// but display the English version just in case it does.
|
|
|
|
return moment(date).from(moment());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off the 's' for the singular suffix, construct the string ID,
|
|
|
|
// then load the localized version.
|
|
|
|
const suffix = duration === 1 ? suffixList[i].substr(0, suffixList[i].length - 1) : suffixList[i];
|
2020-10-05 17:44:38 +02:00
|
|
|
let strId = '%duration% ' + suffix + ' ago';
|
|
|
|
|
|
|
|
if (!suffix) {
|
|
|
|
strId = 'Just now';
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:57:03 +02:00
|
|
|
return __(strId, { duration });
|
|
|
|
}
|
|
|
|
|
2017-09-03 18:35:01 +02:00
|
|
|
render() {
|
2020-07-09 18:55:40 +02:00
|
|
|
const { date, timeAgo, show } = this.props;
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:12:30 +02:00
|
|
|
return <span title={moment(date).format('MMMM Do, YYYY hh:mm A')}>{DateTime.getTimeAgoStr(date)}</span>;
|
2018-10-10 07:22:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 02:52:57 +02:00
|
|
|
return (
|
|
|
|
<span>
|
2020-07-09 18:55:40 +02:00
|
|
|
{date && show === DateTime.SHOW_DATE && moment(date).format('MMMM Do, YYYY')}
|
|
|
|
{date && show === DateTime.SHOW_TIME && moment(date).format('hh:mm A')}
|
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;
|