lbry-desktop/ui/component/dateTime/view.jsx

47 lines
1 KiB
React
Raw Normal View History

// @flow
import React from 'react';
import moment from 'moment';
type Props = {
date?: any,
timeAgo?: boolean,
formatOptions: {},
show?: string,
};
class DateTime extends React.PureComponent<Props> {
static SHOW_DATE = 'date';
static SHOW_TIME = 'time';
static SHOW_BOTH = 'both';
2017-10-08 22:43:56 +02:00
static defaultProps = {
formatOptions: {
month: 'long',
day: 'numeric',
year: 'numeric',
2017-10-08 22:43:56 +02:00
},
};
render() {
const { date, formatOptions, timeAgo } = this.props;
const show = this.props.show || DateTime.SHOW_BOTH;
if (timeAgo) {
return date ? <span>{moment(date).from(moment())}</span> : <span />;
}
return (
<span>
{date &&
(show === DateTime.SHOW_BOTH || show === DateTime.SHOW_DATE) &&
date.toLocaleDateString(undefined, formatOptions)}
{show === DateTime.SHOW_BOTH && ' '}
2019-05-07 23:38:29 +02:00
{date && (show === DateTime.SHOW_BOTH || show === DateTime.SHOW_TIME) && date.toLocaleTimeString()}
{!date && '...'}
</span>
);
}
}
export default DateTime;