react-time-ago

Localized relative date/time formatting (both for past and future dates).
Automatically chooses the right units (seconds, minutes, etc) to format a time interval.
Automatically refreshes itself.
See Demo
Examples:
- just now
- 45s
- 5m
- 15 minutes ago
- 3 hours ago
- in 2 months
- in 5 years
- …
Install
The react-time-ago component uses javascript-time-ago library for generating date/time labels, so both of these packages should be installed:
$ npm install react-time-ago javascript-time-ago --save
If you're not using a bundler then use a standalone version from a CDN.
Use
First, javascript-time-ago must be initialized with some locales:
./src/index.js
import TimeAgo from 'javascript-time-ago'
import en from 'javascript-time-ago/locale/en'
import ru from 'javascript-time-ago/locale/ru'
TimeAgo.addDefaultLocale(en)
TimeAgo.addLocale(ru)
Then, use <ReactTimeAgo/> component:
./src/LastSeen.js
import React from 'react'
import ReactTimeAgo from 'react-time-ago'
export default function LastSeen({ date }) {
return (
<div>
Last seen: <ReactTimeAgo date={date} locale="en-US"/>
</div>
)
}
Hook
If you prefer using a React Hook instead of a React Component, the parameters for it are the same as the props for the React component, except:
tooltip: boolean
component: React.Component
wrapperComponent: React.Component
wrapperProps: object
import { useTimeAgo } from 'react-time-ago'
const result = useTimeAgo(parameters)
Returns an object with properties:
date: Date — Same as the date input parameter. If the input parameter was a timestamp (number) then it gets converted to a Date.
formattedDate: string — Formatted date. Example: "5 min ago".
verboseDate: string — Formatted date (verbose variant). Example: "Thursday, December 20, 2012, 7:00:00 AM GMT+4".
Style
<ReactTimeAgo/> component accepts an optional timeStyle property — it should be a javascript-time-ago style: either a built-in style name (like "round", "round-minute", "twitter", etc) or a custom style object.
<ReactTimeAgo date={date} locale="en-US" timeStyle="twitter"/>
React Native
By default, this component renders a <time/> HTML tag. When using this component in React Native, pass a custom component property:
import React from 'react'
import PropTypes from 'prop-types'
import ReactTimeAgo from 'react-time-ago'
export default function TimeAgo(props) {
return <ReactTimeAgo {...props} component={Time}/>
}
function Time({ date, verboseDate, tooltip, children }) {
return <Text>{children}</Text>
}
Time.propTypes = {
date: PropTypes.instanceOf(Date).isRequired,
verboseDate: PropTypes.string,
tooltip: PropTypes.bool.isRequired,
children: PropTypes.string.isRequired
}
Tooltip
By default, the standard HTML title attribute is used to display a tooltip with the verbose date on mouse over.
For custom tooltip design, a custom tooltip component could be rendered. For that, <ReactTimeAgo/> supports properties:
tooltip={false} — Instructs the component not to add the default HTML title attribute.
wrapperComponent — A React component that's gonna wrap the date/time label. Receives properties: children (Example: <time>2 days ago</time>) and verboseDate: string (Example: "Wednesday, January 1, 2000, 10:45:10 PM").
wrapperProps — If defined, these properties are passed through to the wrapperComponent.
For example, here's how to render a react-responsive-ui/Tooltip:
import React from 'react'
import PropTypes from 'prop-types'
import Tooltip from 'react-responsive-ui/commonjs/Tooltip'
import ReactTimeAgo from 'react-time-ago'
import 'react-time-ago/Tooltip.css'
export default function ReactTimeAgoWithTooltip(props) {
return (
<ReactTimeAgo
{...props}
wrapperComponent={TooltipContainer}
tooltip={false}/>
)
}
const TooltipContainer = ({ verboseDate, children, ...rest }) => (
<Tooltip {...rest} content={verboseDate}>
{children}
</Tooltip>
)
TooltipContainer.propTypes = {
verboseDate: PropTypes.string,
children: PropTypes.node.isRequired
}
Future
When given future dates, .format() produces the corresponding output.: "in 5 minutes", "in 10 days", etc.
To restrict the formatted date to be future-only, pass future property, and it will stop when it reaches "zero point" ("in a moment") and won't allow the date to be formatted as a past one.
<ReactTimeAgo future date={date} .../>
CDN
One can use any npm CDN service, e.g. unpkg.com or jsdelivr.com
<script src="https://unpkg.com/javascript-time-ago@[version]/bundle/javascript-time-ago.js"></script>
<script src="https://unpkg.com/react-time-ago@[version]/bundle/react-time-ago.js"></script>
<script>
TimeAgo.addDefaultLocale({
locale: 'en',
now: {
now: {
current: "now",
future: "in a moment",
past: "just now"
}
},
long: {
year: {
past: {
one: "{0} year ago",
other: "{0} years ago"
},
future: {
one: "in {0} year",
other: "in {0} years"
}
},
...
}
})
</script>
<script>
...
<ReactTimeAgo date={new Date()} locale="en-US" timeStyle="twitter"/>
...
</script>
Props
date: PropTypes.oneOfType([
PropTypes.instanceOf(Date),
PropTypes.number
]).isRequired,
locale: PropTypes.string,
locales: PropTypes.arrayOf(PropTypes.string),
future: PropTypes.bool,
timeStyle: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
]),
round: PropTypes.string,
minTimeLeft: PropTypes.number,
component: PropTypes.elementType,
tooltip: PropTypes.bool,
formatVerboseDate: PropTypes.func,
verboseDateFormat: PropTypes.object,
updateInterval: PropTypes.oneOfType([
PropTypes.number,
PropTypes.arrayOf(PropTypes.shape({
threshold: PropTypes.number,
interval: PropTypes.number.isRequired
}))
]),
tick: PropTypes.bool,
now: PropTypes.number,
timeOffset: PropTypes.number,
polyfill: PropTypes.bool,
wrapperComponent: PropTypes.elementType,
wrapperProps: PropTypes.object
TypeScript
This library comes with TypeScript "typings". If you happen to find any bugs in those, create an issue.
GitHub
On March 9th, 2020, GitHub, Inc. silently banned my account (erasing all my repos, issues and comments) without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. GitHub repo is now deprecated, and the latest source codes can be found on GitLab, which is also the place to report any issues.
License
MIT