react-moment ============ React component for the [moment](http://momentjs.com/) date library. Moment is now a legacy project and is no longer being maintained. As such, this library is no longer being maintained outside of critical bug fixes. [![NPM Downloads](https://img.shields.io/npm/dm/react-moment.svg?style=flat-square)](https://www.npmjs.com/package/react-moment) [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/headzoo/react-moment/master/LICENSE) - [Installing](#installing) - [CDN](#cdn) - [2.0.x](#20x) - [Timezone Support](#timezone-support) - [Quick Start](#quick-start) - [Hooks](#hooks) - [useMomentContent](#usemomentcontent) - [useMomentContext](#usemomentcontext) - [Props](#props) - [MomentProvider](#momentprovider) - [Pooled Timer](#pooled-timer) - [Interval](#interval) - [Output mode precedence](#output-mode-precedence) - [Formatting](#formatting) - [Parsing Dates](#parsing-dates) - [Add and Subtract](#add-and-subtract) - [From Now](#from-now) - [From Now Short](#from-now-short) - [From Now During](#from-now-during) - [From](#from) - [To Now](#to-now) - [To](#to) - [Filters](#filters) - [Custom Renderers](#custom-renderers) - [With Title](#with-title) - [Title Format](#title-format) - [Difference](#difference) - [Duration](#duration) - [Duration From Now](#duration-from-now) - [Unix Timestamps](#unix-timestamps) - [Local](#local) - [Timezone](#timezone) - [Calendar](#calendar) - [Locale](#locale) - [Element](#element) - [OnChange](#onchange) - [Other Props](#other-props) - [Usage with React Native](#usage-with-react-native) - [License](#license) - [Contributors](#contributors) ### Installing Requires [React](https://react.dev/) 18 or later, plus the peer dependency `moment`. Install with npm: ```sh npm install --save moment react-moment ``` #### CDN A pre-built UMD bundle is published alongside the npm package and can be loaded directly from [unpkg](https://unpkg.com/) (or any other npm CDN). Load `react`, `react-dom`, and `moment` first, then `react-moment`. The component is exposed on the global `window['react-moment']`. ```html react-moment via CDN
``` Outputs: ```html ``` ### 2.0.x Version 2.0.x migrates the library to TypeScript and replaces the class-based `Moment` component with a functional component. Changes in 2.0.x: - `prop-types` is no longer a peer dependency (runtime prop validation was removed; types are provided via TypeScript declarations) - `Moment` is a function component — code that relied on class-component internals (for example `instanceof Moment`, instance methods on refs, or `TestUtils.findRenderedComponentWithType`) must be updated See [MIGRATION.md](https://github.com/headzoo/react-moment/blob/master/MIGRATION.md) for the full upgrade guide and breaking-change details. ### Duration Formatting The `moment-duration-format` package is required for the `duration` and `durationFromNow` props. ```sh npm install --save moment-duration-format ``` It is an optional peer dependency — install it only when you use duration formatting. ### Timezone Support The `moment-timezone` package is required to use the timezone related functions. ```sh npm install --save moment-timezone ``` Then import the package into your project. ```jsx import React from 'react'; import Moment from 'react-moment'; import 'moment-timezone'; export default function App() { ... } ``` ### Quick Start ```jsx import React from 'react'; import Moment from 'react-moment'; export default function MyComponent() { const dateToFormat = '1976-04-19T12:59-0500'; return ( {dateToFormat} ); } ``` Outputs: ```html ``` 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 ``. ```jsx import React from 'react'; import Moment from 'react-moment'; export default function MyComponent() { const dateToFormat = '1976-04-19T12:59-0500'; return ( ); } ``` The date value may be a string, object, array, or `Date` instance. ```jsx import React from 'react'; import Moment from 'react-moment'; export default function MyComponent() { const dateToFormat = new Date('1976-04-19T12:59-0500'); return ( ); } ``` ### Hooks Use hooks when you need formatted output without rendering a `