Skip to content

Commit

Permalink
Merge pull request #143 from uptick/feature/relative-datetime
Browse files Browse the repository at this point in the history
Add RelativeDateTime component
  • Loading branch information
Sticky-Bits authored Jun 30, 2022
2 parents 3d2b33c + 021caa2 commit bba86bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/types/RelativeDateTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react'
import PropTypes from 'prop-types'
import Moment from 'moment'
import DatePart from './DatePart'
import Empty from './Empty'
import {SHORTDATETIME_FORMAT} from '../utils'

/**
* Displays a datetime relative to the current time eg. "4 days ago" or "in 3 hours"
* alongside the datetime passed in
*/
class RelativeDateTime extends React.Component {
static propTypes = {
value: PropTypes.string,
}

render() {
const date = Moment(this.props.value)
const isValid = date.isValid()
if (isValid) {
return (
<div>
<div>
{Moment(this.props.value).fromNow()}
</div>
<div className="text-muted">
<DatePart value={this.props.value} outputFormat={SHORTDATETIME_FORMAT} />
</div>
</div>
)
}
return (<Empty />)
}
}
export default RelativeDateTime
1 change: 1 addition & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export DatePart from './DatePart'
export DateTime from './DateTime'
export BooleanType from './BooleanType'
export RelativeDate from './RelativeDate'
export RelativeDateTime from './RelativeDateTime'
export Currency from './Currency'

0 comments on commit bba86bc

Please sign in to comment.