Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
react-moment
Advanced tools
The react-moment npm package is a React component for the Moment.js library, which allows you to easily format, parse, and manipulate dates and times in your React applications.
Formatting Dates
This feature allows you to format dates in various ways. In this example, the date will be formatted as 'YYYY/MM/DD'.
<Moment format='YYYY/MM/DD'>{dateToFormat}</Moment>
Relative Time
This feature allows you to display dates in a relative format, such as 'a few seconds ago' or 'in 3 days'.
<Moment fromNow>{dateToFormat}</Moment>
Unix Timestamps
This feature allows you to convert Unix timestamps to human-readable dates.
<Moment unix>{unixTimestamp}</Moment>
Custom Calendar Time
This feature allows you to display dates in a custom calendar format, such as 'Today at 2:30 PM' or 'Last Monday at 4:00 PM'.
<Moment calendar>{dateToFormat}</Moment>
Duration
This feature allows you to display durations in a specified format, such as 'hh:mm:ss'.
<Moment duration={duration} format='hh:mm:ss'/>
date-fns is a modern JavaScript date utility library that provides a comprehensive set of functions for manipulating dates. Unlike react-moment, date-fns is not tied to React and can be used in any JavaScript project. It is known for its functional programming style and tree-shakable modules.
dayjs is a lightweight JavaScript date library that is a modern alternative to Moment.js. It has a similar API to Moment.js but is much smaller in size. Like react-moment, dayjs can be used to format, parse, and manipulate dates, but it is not specifically designed for React.
luxon is a powerful, modern JavaScript date and time library built by one of the Moment.js developers. It offers a more comprehensive and immutable API compared to Moment.js. Luxon is not specifically designed for React but can be easily integrated into React applications.
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
.
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 />
);
}
}
Outputs:
<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.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment parse="YYYY-MM-DD HH:mm">
1976-04-19 12:59
</Moment>
);
}
}
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.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
const date = new Date();
return (
<div>
<Moment add={{ hours: 12 }}>{date}</Moment>
<Moment add={{ days: 1, hours: 12 }}>{date}</Moment>
<Moment subtract={{ hours: 12 }}>{date}</Moment>
<Moment subtract={{ days: 1, hours: 12 }}>{date}</Moment>
</div>
);
}
}
fromNow={bool}
Sometimes called timeago or relative time, displays the date as the time from now, e.g. "5 minutes ago".
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment fromNow>1976-04-19T12:59-0500</Moment>
);
}
}
Outputs:
<time>40 years ago</time>
Including ago
with fromNow
will omit the suffix from the relative time.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment fromNow ago>1976-04-19T12:59-0500</Moment>
);
}
}
Outputs:
<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}
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment from="2015-04-19">1976-04-19T12:59-0500</Moment>
);
}
}
Outputs:
<time>39 years</time>
toNow={bool}
Similar to fromNow
, but gives the opposite interval.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment toNow>1976-04-19T12:59-0500</Moment>
);
}
}
Outputs:
<time>40 years ago</time>
to={string}
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment to="2015-04-19">1976-04-19T12:59-0500</Moment>
);
}
}
Outputs:
<time>39 years</time>
filter={function}
A function which modifies/transforms the date value prior to rendering.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
const toUpperCaseFilter = (d) => {
return d.toUpperCase();
};
return (
const dateToFormat = '1976-04-19T12:59-0500';
<Moment filter={toUpperCaseFilter}>{dateToFormat}</Moment>
);
}
}
Outputs:
<time>MON APR 19 1976 12:59:00 GMT-0500</time>
withTitle={bool}
Adds a title
attribute to the element with the complete date.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment format="D MMM YYYY" withTitle>
1976-04-19T12:59-0500
</Moment>
);
}
}
Outputs:
<time title="1976-04-19T12:59-0500">19 Apr 1976</time>
titleFormat={string}
How the title
date is formatted when using the withTitle
attribute.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment titleFormat="D MMM YYYY" withTitle>
1976-04-19T12:59-0500
</Moment>
);
}
}
Outputs:
<time title="19 Apr 1976">1976-04-19T12:59-0500</time>
diff={string}
decimal={bool}
unit={string}
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<div>
<Moment diff="2015-04-19">1976-04-19T12:59-0500</Moment>
<Moment diff="2015-04-19" unit="days">1976-04-19T12:59-0500</Moment>
<Moment diff="2015-04-19" unit="years" decimal>1976-04-19T12:59-0500</Moment>
</div>
);
}
}
duration={string}
date={string}
Shows the duration (elapsed time) between two dates. duration
property should be behind date
property time-wise.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment duration="2018-11-1T10:59-0500"
date="2018-11-1T12:59-0500"
/>
);
}
}
durationFromNow={bool}
Shows the duration (elapsed time) between now and the provided datetime.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment date="2018-11-1T12:59-0500"
durationFromNow
/>
);
}
}
unix={bool}
Tells Moment to parse the given date value as a unix timestamp.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
const unixTimestamp = 198784740;
return (
<Moment unix>{unixTimestamp}</Moment>
);
}
}
Outputs:
<time>Mon Apr 19 1976 12:59:00 GMT-0500</time>
local={bool}
Outputs the result in local time.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment local>
2018-11-01T12:59-0500
</Moment>
);
}
}
Outputs:
<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.
import React from 'react';
import Moment from 'react-moment';
import 'moment-timezone';
export default class MyComponent extends React.Component {
render() {
const unixTimestamp = 198784740;
return (
<Moment unix tz="America/Los_Angeles">
{unixTimestamp}
</Moment>
);
}
}
Outputs:
<time>Mon Apr 19 1976 09:59:00 GMT-0800</time>
calendar={object|bool}
Customize the strings used for the calendar function.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
const calendarStrings = {
lastDay : '[Yesterday at] LT',
sameDay : '[Today at] LT',
nextDay : '[Tomorrow at] LT',
lastWeek : '[last] dddd [at] LT',
nextWeek : 'dddd [at] LT',
sameElse : 'L'
};
return (
<Moment calendar={calendarStrings}>
'1976-04-19T12:59-0500'
</Moment>
);
}
}
locale={string}
Sets the locale used to display the date.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
const dateToFormat = '1976-04-19T12:59-0500';
return (
<Moment locale="de">{dateToFormat}</Moment>
);
}
}
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.
import 'moment/locale/fr';
element={string|React.Component}
The element type to render as (string or function).
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment element="span">1976-04-19T12:59-0500</Moment>
);
}
}
Outputs:
<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.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment onChange={(val) => { console.log(val); }}>
1976-04-19T12:59-0500
</Moment>
);
}
}
Any other properties are passed to the <time>
element.
import React from 'react';
import Moment from 'react-moment';
export default class MyComponent extends React.Component {
render() {
return (
<Moment className="datetime" aria-hidden={true}>
1976-04-19T12:59-0500
</Moment>
);
}
}
Outputs:
<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.
import React from 'react';
import ReactDOM from 'react-dom';
import Moment from 'react-moment';
import App from './components/app';
// Start the pooled timer which runs every 60 seconds
// (60000 milliseconds) by default.
Moment.startPooledTimer();
// Or set the update interval. This will update the mounted
// instances every 30 seconds.
// Moment.startPooledTimer(30000);
ReactDOM.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.
import React from 'react';
import ReactDOM from 'react-dom';
import moment from 'moment/min/moment-with-locales';
import Moment from 'react-moment';
// Sets the moment instance to use.
Moment.globalMoment = moment;
// Set the locale for every react-moment instance to French.
Moment.globalLocale = 'fr';
// Set the output format for every react-moment instance.
Moment.globalFormat = 'D MMM YYYY';
// Set the timezone for every instance.
Moment.globalTimezone = 'America/Los_Angeles';
// Set the output timezone for local for every instance.
Moment.globalLocal = true;
// Use a <span> tag for every react-moment instance.
Moment.globalElement = 'span';
// Upper case all rendered dates.
Moment.globalFilter = (d) => {
return d.toUpperCase();
};
const App = () => (
<Moment>1976-04-19T12:59-0500</Moment>
);
ReactDOM.render(<App />, document.getElementById('mount'));
You can override the global values on a per-instance basis using regular props.
import React from 'react';
import ReactDOM from 'react-dom';
import Moment from 'react-moment';
// Set the locale for every react-moment instance to French.
Moment.globalLocale = 'fr';
const App = () => (
<div>
{/*
Renders using the 'fr' locale due to the global setting.
*/}
<Moment>1976-04-19T12:59-0500</Moment>
{/*
Overrides the global locale and uses 'en' instead.
*/}
<Moment locale="en">1976-04-19T12:59-0500</Moment>
</div>
);
ReactDOM.render(<App />, document.getElementById('mount'));
If you are using React Native then you'll have to pass in Text
.
import Moment from 'react-moment';
import { Text } from 'react-native';
Then:
<Moment element={Text} >1976-04-19T12:59-0500</Moment>
This software is released under the MIT license. See LICENSE for more details.
FAQs
React component for the moment date library.
The npm package react-moment receives a total of 109,694 weekly downloads. As such, react-moment popularity was classified as popular.
We found that react-moment demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.