React Moment Component

1.2.2 · maintenance · verified Sun Apr 19

react-moment is a React component that acts as a wrapper for the widely-used `moment.js` date and time library. It enables developers to easily display formatted dates and times within React applications, leveraging Moment's extensive parsing, formatting, and manipulation capabilities. The current stable version, 1.2.2, integrates with `moment.js` versions 2.29.0 and higher. While `moment.js` itself is now considered a legacy project with its maintainers recommending modern alternatives like Luxon, date-fns, or Day.js, `react-moment` remains functional for projects still relying on the Moment.js ecosystem. It provides a declarative, component-based API for Moment's features, including relative time ("from now"), calendrical formatting, and custom date displays, often with built-in interval updates. The library also ships with TypeScript types, enhancing the developer experience in typed React projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the `Moment` component for various date and time displays, including custom formatting, relative time with live updates, and calendar views.

import React from 'react';
import Moment from 'react-moment';
import type { FC } from 'react';

const App: FC = () => {
  const dateToFormat = '1976-04-19T12:59-0500';
  // Use a dynamic date for 'fromNow' to show updates.
  // Example: 1 hour from now
  const futureDate = new Date(Date.now() + 60 * 60 * 1000);

  return (
    <div>
      <h1>React Moment Examples</h1>
      <p>Original date: <code>{dateToFormat}</code></p>
      <p>Standard format: <Moment date={dateToFormat} /></p>
      <p>Custom format: <Moment date={futureDate} format="MMMM Do YYYY, h:mm:ss a" /></p>
      {/* fromNow updates automatically by default every 60s, explicitly setting interval */}
      <p>Relative time (from now, updates every 30s): <Moment fromNow interval={30000}>{futureDate}</Moment></p>
      <p>Calendar time (e.g., Today at 3:00 PM): <Moment calendar>{new Date()}</Moment></p>
      <p>Just the current time: <Moment date={new Date()} format="HH:mm:ss" interval={1000} /></p>
      <p>Date passed as child: <Moment>{dateToFormat}</Moment></p>
    </div>
  );
};

export default App;

view raw JSON →