react-moment
React component for the moment date library.
- Installing
- Quick Start
- Formatting
- Parsing Dates
- From Now
- From
- To Now
- To
- Unix Timestamps
- Timezone
- Locale
- Other Props
- License
- Contributors
Installing
Use npm to install react-moment, along with its peer dependencies, moment
and moment-timezone
.
npm install --save moment moment-timezone react-moment
Quick Start
;; exports default Component { let dateToFormat = '1976-04-19T12:59-0500'; <Moment>dateToFormat</Moment> }
Outputs:
Mon Apr 19 1976 12:59:00 GMT-0500
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>
.
;; exports default Component { let dateToFormat = '1976-04-19T12:59-0500'; <Moment date=dateToFormat /> }
The date value may be a string, object, array, or Date
instance.
;; exports default Component { let dateToFormat = '1976-04-19T12:59-0500'; <Moment date=dateToFormat /> }
Formatting
;; exports default Component { <Moment format="YYYY/MM/DD">1976-04-19T12:59-0500</Moment> }
Outputs:
1976/04/19
Parsing Dates
Moment can parse most standard date formats. Use the parse
attribute
to tell moment how to parse the given date when non-standard.
;; exports default Component { <Moment parse="YYYY-MM-DD HH:mm">1976-04-19 12:59</Moment> }
From Now
;; exports default Component { <Moment fromNow>1976-04-19T12:59-0500</Moment> }
Outputs:
40 years
;; exports default Component { <Moment fromNow ago>1976-04-19T12:59-0500</Moment> }
Outputs:
40 years
From
;; exports default Component { <Moment from="2015-04-19">1976-04-19T12:59-0500</Moment> }
Outputs:
39 years
;; exports default Component { <Moment from="2015-04-19" ago>1976-04-19T12:59-0500</Moment> }
Outputs:
39 years
To Now
;; exports default Component { <Moment toNow>1976-04-19T12:59-0500</Moment> }
Outputs:
40 years
;; exports default Component { <Moment toNow ago>1976-04-19T12:59-0500</Moment> }
Outputs:
in 40 years
To
;; exports default Component { <Moment to="2015-04-19">1976-04-19T12:59-0500</Moment> }
Outputs:
39 years
;; exports default Component { <Moment to="2015-04-19" ago>1976-04-19T12:59-0500</Moment> }
Outputs:
in 39 years
Unix Timestamps
;; exports default Component { let unixTimestamp = 198784740; <Moment unix>unixTimestamp</Moment> }
Outputs:
Mon Apr 19 1976 12:59:00 GMT-0500
Timezone
To enable server side rendering (SSR), client and server has to provide same datetime, based on common Timezone.
tz
attribute will enable set the common timezone.
;; exports default Component { let unixTimestamp = 198784740; <Moment unix tz="America/Los_Angeles">unixTimestamp</Moment> }
Outputs:
Mon Apr 19 1976 09:59:00 GMT-0800
Locale
;; exports default Component { let dateToFormat = '1976-04-19T12:59-0500'; <Moment locale="de">dateToFormat</Moment> }
Other Props
Any other properties are passed to the <time>
element.
;; exports default Component { <Moment className="datetime" aria-hidden=true>1976-04-19T12:59-0500</Moment> }
Outputs:
Mon Apr 19 1976 12:59:00 GMT-0500
License
This software is released under the MIT license. See LICENSE for more details.