How to install Moment.js

Created

Moment.js was designed to work both in the browser and in Node.js.

All code should work in both of these environments, and all unit tests are run in both of these environments.

Currently, the following browsers are used for the ci system: Chrome on Windows XP, IE 8, 9, and 10 on Windows 7, IE 11 on Windows 10, latest Firefox on Linux, and latest Safari on OSX 10.8 and 10.11.

Installation

You can download the latest version of Moment.js from the GitHub releases or use a Moment.js CDN.

Cloning a repository

You can clone a repository from GitHub.com to your local computer.
Cloning with HTTPS URLs
git clone https://github.com/moment/moment.git
Cloning with SSH URLs
git clone [email protected]:moment/moment.git
Cloning with GitHub CLI
gh repo clone moment/moment

Install via NPM package

Run the following command to locally install the package and its dependencies with NPM.
Downloading and installing packages locally
npm i moment
npm install moment
yarn add moment
Downloading and installing packages globally
npm i -g moment
yarn global add moment
Install Specific Version of a Package
npm install [email protected]
Bower
bower install moment

Example

Code 1: - If you want to try the sample codes below, just open your browser's console and enter them.

import moment from 'moment';

// Format Dates
moment().format('MMMM Do YYYY, h:mm:ss a'); // March 7th 2021, 3:32:29 pm
moment().format('dddd');                    // Sunday
moment().format("MMM Do YY");               // Mar 7th 21
moment().format('YYYY [escaped] YYYY');     // 2021 escaped 2021
moment().format();                          // 2021-03-07T15:32:29+08:00

// Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 9 years ago
moment().startOf('day').fromNow();        // 16 hours ago
moment().endOf('day').fromNow();          // in 8 hours
moment().startOf('hour').fromNow();       // 34 minutes ago

// Calendar Time
moment().subtract(10, 'days').calendar(); // 02/25/2021
moment().subtract(6, 'days').calendar();  // Last Monday at 3:34 PM
moment().subtract(3, 'days').calendar();  // Last Thursday at 3:34 PM
moment().subtract(1, 'days').calendar();  // Yesterday at 3:34 PM
moment().calendar();                      // Today at 3:34 PM
moment().add(1, 'days').calendar();       // Tomorrow at 3:34 PM
moment().add(3, 'days').calendar();       // Wednesday at 3:34 PM
moment().add(10, 'days').calendar();      // 03/17/2021

// Multiple Locale Support
moment.locale();         // en
moment().format('LT');   // 3:34 PM
moment().format('LTS');  // 3:34:53 PM
moment().format('L');    // 03/07/2021
moment().format('l');    // 3/7/2021
moment().format('LL');   // March 7, 2021
moment().format('ll');   // Mar 7, 2021
moment().format('LLL');  // March 7, 2021 3:34 PM
moment().format('lll');  // Mar 7, 2021 3:34 PM
moment().format('LLLL'); // Sunday, March 7, 2021 3:34 PM
moment().format('llll'); // Sun, Mar 7, 2021 3:34 PM