forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
43 lines (39 loc) · 1.13 KB
/
index.jsx
File metadata and controls
43 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Component } from 'react';
import {
isDate,
formatDistance,
formatDistanceStrict,
parseISO,
} from 'date-fns';
import { date } from '../../utils/prop-types';
/**
* Display a human-readable relative string between a date and now.
* Optionally also show a relative distance between that date and an
* additional offset date.
*/
export default class DateDistance extends Component {
static defaultProps = {
offset: null,
};
static propTypes = {
/**
* The origin date for which to render a relative string from now.
*/
from: date.isRequired,
/**
* An optional date for which to also show a relative string between `from`
* and `offset`.
*/
offset: date,
};
render() {
const { from, offset } = this.props;
const fromParsed = isDate(from) ? from : parseISO(from);
const fromNow = formatDistanceStrict(fromParsed, new Date(), {
addSuffix: true,
});
const offsetParsed = isDate(offset) ? offset : parseISO(offset);
const offsetNow = offset && formatDistance(fromParsed, offsetParsed);
return offsetNow ? `${fromNow} (${offsetNow} later)` : fromNow;
}
}