React component for the moment date library.
- Installing
- Timezone Support
- Quick Start
- Props
- Pooled Timer
- Global Config
- Usage with React Native
- License
- Contributors
Node 8 or greater is required. Use npm to install react-moment
along with its peer dependency, moment
.
npm install --save moment react-moment
The moment-timezone
package is required to use the timezone related functions.
npm install --save moment-timezone
Then import the package into your project.
import React from 'react';
import Moment from 'react-moment';
import 'moment-timezone';
export default class App extends React.Component {
...
}
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
const dateToFormat = '1976-04-19T12:59-0500';
<Moment>{dateToFormat}</Moment>
);
}
}
Outputs:
<time>Mon Apr 19 1976 12:59:00 GMT-0500</time>
The above example could also be written this way if you prefer to pass the date using an attribute rather than as a child to <Moment>
.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
const dateToFormat = '1976-04-19T12:59-0500';
<Moment date={dateToFormat} />
);
}
}
The date value may be a string, object, array, or Date
instance.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
const dateToFormat = new Date('1976-04-19T12:59-0500');
<Moment date={dateToFormat} />
);
}
}
The component supports the following props. See the Moment docs for more information.
interval={number}
By default the time updates every 60 seconds (60000 milliseconds). Use the interval
prop to change or disable updating.
Updates the time every 30 seconds (30000 milliseconds).
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment interval={30000}>
1976-04-19T12:59-0500
</Moment>
);
}
}
Disables updating.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment interval={0}>
1976-04-19T12:59-0500
</Moment>
);
}
}
format={string}
Formats the date according to the given format string. See the Moment docs on formatting for more information.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment format="YYYY/MM/DD">
1976-04-19T12:59-0500
</Moment>
);
}
}
Outputs:
<time>1976/04/19</time>
For Duration and DurationFromNow formatting, the formatting is done using a separate library. See the Moment-Duration-Format docs on formatting for more information.
import React from 'react';
import Moment from 'react-moment';
import moment from 'moment';
export default class MyComponent extends React.Component {
const start = moment().add(-4, 'm');
render() {
return (
<Moment date={start} format="hh:mm:ss" durationFromNow />
);
}
}
Outputs:
<time>00:04:00</time>
trim={bool}
When formatting duration time, the largest-magnitude tokens are automatically trimmed when they have no value. See the Moment-Duration-Format docs on trim for more information.
import React from 'react';
import Moment from 'react-moment';
import moment from 'moment';
export default class MyComponent extends React.Component {
const start = moment().add(-4, 'm');
render() {
return (
<Moment date={start} format="hh:mm:ss" trim durationFromNow />
);
}
}