Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit bba86bc

Browse files
authored
Merge pull request #143 from uptick/feature/relative-datetime
Add RelativeDateTime component
2 parents 3d2b33c + 021caa2 commit bba86bc

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/types/RelativeDateTime.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import Moment from 'moment'
4+
import DatePart from './DatePart'
5+
import Empty from './Empty'
6+
import {SHORTDATETIME_FORMAT} from '../utils'
7+
8+
/**
9+
* Displays a datetime relative to the current time eg. "4 days ago" or "in 3 hours"
10+
* alongside the datetime passed in
11+
*/
12+
class RelativeDateTime extends React.Component {
13+
static propTypes = {
14+
value: PropTypes.string,
15+
}
16+
17+
render() {
18+
const date = Moment(this.props.value)
19+
const isValid = date.isValid()
20+
if (isValid) {
21+
return (
22+
<div>
23+
<div>
24+
{Moment(this.props.value).fromNow()}
25+
</div>
26+
<div className="text-muted">
27+
<DatePart value={this.props.value} outputFormat={SHORTDATETIME_FORMAT} />
28+
</div>
29+
</div>
30+
)
31+
}
32+
return (<Empty />)
33+
}
34+
}
35+
export default RelativeDateTime

src/types/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export DatePart from './DatePart'
77
export DateTime from './DateTime'
88
export BooleanType from './BooleanType'
99
export RelativeDate from './RelativeDate'
10+
export RelativeDateTime from './RelativeDateTime'
1011
export Currency from './Currency'

0 commit comments

Comments
 (0)