Gathering detailed insights and metrics for react-moment
Gathering detailed insights and metrics for react-moment
Gathering detailed insights and metrics for react-moment
Gathering detailed insights and metrics for react-moment
npm install react-moment
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
517 Stars
245 Commits
72 Forks
11 Watching
24 Branches
22 Contributors
Updated on 16 Sept 2024
Minified
Minified + Gzipped
JavaScript (99.36%)
HTML (0.64%)
Cumulative downloads
Total Downloads
Last day
-1.3%
28,013
Compared to previous day
Last week
5.4%
145,597
Compared to previous week
Last month
7.5%
611,419
Compared to previous month
Last year
-29.9%
7,488,443
Compared to previous year
3
34
React component for the moment date library.
Node 8 or greater is required. Use npm to install react-moment
along with its peer dependency, moment
.
1npm install --save moment react-moment
The moment-timezone
package is required to use the timezone related functions.
1npm install --save moment-timezone
Then import the package into your project.
1import React from 'react'; 2import Moment from 'react-moment'; 3import 'moment-timezone'; 4 5export default class App extends React.Component { 6 ... 7}
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 const dateToFormat = '1976-04-19T12:59-0500'; 8 <Moment>{dateToFormat}</Moment> 9 ); 10 } 11}
Outputs:
1<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>
.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 const dateToFormat = '1976-04-19T12:59-0500'; 8 <Moment date={dateToFormat} /> 9 ); 10 } 11}
The date value may be a string, object, array, or Date
instance.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 const dateToFormat = new Date('1976-04-19T12:59-0500'); 8 <Moment date={dateToFormat} /> 9 ); 10 } 11}
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).
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment interval={30000}> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
Disables updating.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment interval={0}> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
format={string}
Formats the date according to the given format string. See the Moment docs on formatting for more information.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment format="YYYY/MM/DD"> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
Outputs:
1<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.
1import React from 'react'; 2import Moment from 'react-moment'; 3import moment from 'moment'; 4 5export default class MyComponent extends React.Component { 6 const start = moment().add(-4, 'm'); 7 render() { 8 return ( 9 <Moment date={start} format="hh:mm:ss" durationFromNow /> 10 ); 11 } 12}
Outputs:
1<time>00:04:00</time>
trim={string|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.
1import React from 'react'; 2import Moment from 'react-moment'; 3import moment from 'moment'; 4 5export default class MyComponent extends React.Component { 6 const start = moment().add(-4, 'm'); 7 render() { 8 return ( 9 <Moment date={start} format="hh:mm:ss" trim durationFromNow /> 10 ); 11 } 12}
Outputs:
1<time>04:00</time>
parse={string}
Moment can parse most standard date formats. Use the parse
attribute to tell moment how to parse the given date when non-standard. See the Moment docs on parsing for more information.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment parse="YYYY-MM-DD HH:mm"> 8 1976-04-19 12:59 9 </Moment> 10 ); 11 } 12}
add={object}
subtract={object}
Used to add and subtract periods of time from the given date, with the time periods expressed as object literals. See the Moment docs on add and subtract for more information.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 const date = new Date(); 7 8 return ( 9 <div> 10 <Moment add={{ hours: 12 }}>{date}</Moment> 11 <Moment add={{ days: 1, hours: 12 }}>{date}</Moment> 12 <Moment subtract={{ hours: 12 }}>{date}</Moment> 13 <Moment subtract={{ days: 1, hours: 12 }}>{date}</Moment> 14 </div> 15 ); 16 } 17}
fromNow={bool}
Sometimes called timeago or relative time, displays the date as the time from now, e.g. "5 minutes ago".
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment fromNow>1976-04-19T12:59-0500</Moment> 8 ); 9 } 10}
Outputs:
1<time>40 years ago</time>
Including ago
with fromNow
will omit the suffix from the relative time.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment fromNow ago>1976-04-19T12:59-0500</Moment> 8 ); 9 } 10}
Outputs:
1<time>40 years</time>
fromNowDuring={number}
Setting fromNowDuring will display the relative time as with fromNow but just during its value in milliseconds, after that format will be used instead.
from={string}
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment from="2015-04-19">1976-04-19T12:59-0500</Moment> 8 ); 9 } 10}
Outputs:
1<time>39 years</time>
toNow={bool}
Similar to fromNow
, but gives the opposite interval.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment toNow>1976-04-19T12:59-0500</Moment> 8 ); 9 } 10}
Outputs:
1<time>40 years ago</time>
to={string}
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment to="2015-04-19">1976-04-19T12:59-0500</Moment> 8 ); 9 } 10}
Outputs:
1<time>39 years</time>
filter={function}
A function which modifies/transforms the date value prior to rendering.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 const toUpperCaseFilter = (d) => { 7 return d.toUpperCase(); 8 }; 9 10 return ( 11 const dateToFormat = '1976-04-19T12:59-0500'; 12 <Moment filter={toUpperCaseFilter}>{dateToFormat}</Moment> 13 ); 14 } 15}
Outputs:
1<time>MON APR 19 1976 12:59:00 GMT-0500</time>
withTitle={bool}
Adds a title
attribute to the element with the complete date.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment format="D MMM YYYY" withTitle> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
Outputs:
1<time title="1976-04-19T12:59-0500">19 Apr 1976</time>
titleFormat={string}
How the title
date is formatted when using the withTitle
attribute.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment titleFormat="D MMM YYYY" withTitle> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
Outputs:
1<time title="19 Apr 1976">1976-04-19T12:59-0500</time>
diff={string}
decimal={bool}
unit={string}
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <div> 8 <Moment diff="2015-04-19">1976-04-19T12:59-0500</Moment> 9 <Moment diff="2015-04-19" unit="days">1976-04-19T12:59-0500</Moment> 10 <Moment diff="2015-04-19" unit="years" decimal>1976-04-19T12:59-0500</Moment> 11 </div> 12 ); 13 } 14}
duration={string}
date={string}
Shows the duration (elapsed time) between two dates. duration
property should be behind date
property time-wise.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment duration="2018-11-1T10:59-0500" 8 date="2018-11-1T12:59-0500" 9 /> 10 ); 11 } 12}
durationFromNow={bool}
Shows the duration (elapsed time) between now and the provided datetime.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment date="2018-11-1T12:59-0500" 8 durationFromNow 9 /> 10 ); 11 } 12}
unix={bool}
Tells Moment to parse the given date value as a unix timestamp.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 const unixTimestamp = 198784740; 7 return ( 8 <Moment unix>{unixTimestamp}</Moment> 9 ); 10 } 11}
Outputs:
1<time>Mon Apr 19 1976 12:59:00 GMT-0500</time>
local={bool}
Outputs the result in local time.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment local> 8 2018-11-01T12:59-0500 9 </Moment> 10 ); 11 } 12}
Outputs:
1<time>Thu Nov 01 2018 18:59:00 GMT+0100</time>
tz={string}
Sets the timezone. To enable server side rendering (SSR), client and server has to provide same datetime, based on common Timezone. The tz
attribute will enable set the common timezone.
1import React from 'react'; 2import Moment from 'react-moment'; 3import 'moment-timezone'; 4 5export default class MyComponent extends React.Component { 6 render() { 7 const unixTimestamp = 198784740; 8 return ( 9 <Moment unix tz="America/Los_Angeles"> 10 {unixTimestamp} 11 </Moment> 12 ); 13 } 14}
Outputs:
1<time>Mon Apr 19 1976 09:59:00 GMT-0800</time>
calendar={object|bool}
Customize the strings used for the calendar function.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 const calendarStrings = { 7 lastDay : '[Yesterday at] LT', 8 sameDay : '[Today at] LT', 9 nextDay : '[Tomorrow at] LT', 10 lastWeek : '[last] dddd [at] LT', 11 nextWeek : 'dddd [at] LT', 12 sameElse : 'L' 13 }; 14 15 return ( 16 <Moment calendar={calendarStrings}> 17 '1976-04-19T12:59-0500' 18 </Moment> 19 ); 20 } 21}
locale={string}
Sets the locale used to display the date.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 const dateToFormat = '1976-04-19T12:59-0500'; 7 return ( 8 <Moment locale="de">{dateToFormat}</Moment> 9 ); 10 } 11}
Note In some cases the language file is not automatically loaded by moment, and it must be manually loaded. For example, to use the French locale, add the following to your bootstrap (e.g. index.js) script.
1import 'moment/locale/fr';
element={string|React.Component}
The element type to render as (string or function).
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment element="span">1976-04-19T12:59-0500</Moment> 8 ); 9 } 10}
Outputs:
1<span>Mon Apr 19 1976 12:59:00 GMT-0500</span>
onChange={func}
The onChange
prop is called each time the date is updated, which by default is every 60 seconds. The function receives the new date value.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment onChange={(val) => { console.log(val); }}> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
Any other properties are passed to the <time>
element.
1import React from 'react'; 2import Moment from 'react-moment'; 3 4export default class MyComponent extends React.Component { 5 render() { 6 return ( 7 <Moment className="datetime" aria-hidden={true}> 8 1976-04-19T12:59-0500 9 </Moment> 10 ); 11 } 12}
Outputs:
1<time class="datetime" aria-hidden="true">Mon Apr 19 1976 12:59:00 GMT-0500</time>
By default a timer is created for each mounted <Moment />
instance to update the date value, which is fine when you only have a few instances on the page. However, performance can take a hit when you have many mounted instance. The problem is solved by using a pooled timer.
When pooled timing is enabled, react-moment will only use a single timer to update all mounted <Moment />
instances. Pooled timing is enabled by calling startPooledTimer()
and stopped by calling clearPooledTimer()
.
Call the startPooledTimer()
static method from your bootstrapping script (usually index.js) to initialize the timer.
1import React from 'react'; 2import ReactDOM from 'react-dom'; 3import Moment from 'react-moment'; 4import App from './components/app'; 5 6// Start the pooled timer which runs every 60 seconds 7// (60000 milliseconds) by default. 8Moment.startPooledTimer(); 9 10// Or set the update interval. This will update the mounted 11// instances every 30 seconds. 12// Moment.startPooledTimer(30000); 13 14ReactDOM.render(<App />, document.getElementById('mount'));
Note: The interval
prop set on each <Moment />
instance is ignored when using pooled timing, except where interval={0}
to disable updating.
Note: The startPooledTimer()
method must be called before any <Moment />
instances are mounted.
Some prop values may be set globally so you don't have to set them on every react-moment instance.
1import React from 'react'; 2import ReactDOM from 'react-dom'; 3import moment from 'moment/min/moment-with-locales'; 4import Moment from 'react-moment'; 5 6// Sets the moment instance to use. 7Moment.globalMoment = moment; 8 9// Set the locale for every react-moment instance to French. 10Moment.globalLocale = 'fr'; 11 12// Set the output format for every react-moment instance. 13Moment.globalFormat = 'D MMM YYYY'; 14 15// Set the timezone for every instance. 16Moment.globalTimezone = 'America/Los_Angeles'; 17 18// Set the output timezone for local for every instance. 19Moment.globalLocal = true; 20 21// Use a <span> tag for every react-moment instance. 22Moment.globalElement = 'span'; 23 24// Upper case all rendered dates. 25Moment.globalFilter = (d) => { 26 return d.toUpperCase(); 27}; 28 29const App = () => ( 30 <Moment>1976-04-19T12:59-0500</Moment> 31); 32 33ReactDOM.render(<App />, document.getElementById('mount'));
You can override the global values on a per-instance basis using regular props.
1import React from 'react'; 2import ReactDOM from 'react-dom'; 3import Moment from 'react-moment'; 4 5// Set the locale for every react-moment instance to French. 6Moment.globalLocale = 'fr'; 7 8const App = () => ( 9 <div> 10 {/* 11 Renders using the 'fr' locale due to the global setting. 12 */} 13 <Moment>1976-04-19T12:59-0500</Moment> 14 15 {/* 16 Overrides the global locale and uses 'en' instead. 17 */} 18 <Moment locale="en">1976-04-19T12:59-0500</Moment> 19 </div> 20); 21 22ReactDOM.render(<App />, document.getElementById('mount'));
If you are using React Native then you'll have to pass in Text
.
1import Moment from 'react-moment'; 2import { Text } from 'react-native';
Then:
1<Moment element={Text} >1976-04-19T12:59-0500</Moment>
This software is released under the MIT license. See LICENSE for more details.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/20 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
121 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More