Moment.js has been successfully used in millions of projects with over 12 million downloads per week.

Here’s a curated moment.js cheatsheet to quickly use in your project.

First, Install Dependencies

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor
bower install moment --save # bower (deprecated)

Date Formats

moment().format('MMMM Do YYYY, h:mm:ss a'); // July 22nd 2022, 12:51:15 pm
moment().format('dddd');                    // Friday
moment().format("MMM Do YY");               // Jul 22nd 22
moment().format('YYYY [escaped] YYYY');     // 2022 escaped 2022
moment().format();                         

Relative Time

moment("20111031", "YYYYMMDD").fromNow(); // 11 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 10 years ago
moment().startOf('day').fromNow();        // 13 hours ago
moment().endOf('day').fromNow();          // in 11 hours
moment().startOf('hour').fromNow();      

Multiple Locale Support

moment.locale();         // en
moment().format('LT');   // 1:04 PM
moment().format('LTS');  // 1:04:27 PM
moment().format('L');    // 07/22/2022
moment().format('l');    // 7/22/2022
moment().format('LL');   // July 22, 2022
moment().format('ll');   // Jul 22, 2022
moment().format('LLL');  // July 22, 2022 1:04 PM
moment().format('lll');  // Jul 22, 2022 1:04 PM
moment().format('LLLL'); // Friday, July 22, 2022 1:04 PM
moment().format('llll'); // Fri Jul 22, 2022 1.05 PM

Visit the momemnt.js website for more information.

See also:

Javascript cheatsheet reference

Author

Recent Posts

Google Launches Its Own ‘Reasoning’ AI Model to Compete with OpenAI

This month has been packed for Google as it ramps up efforts to outshine OpenAI…

2 days ago

You can now use your phone line to call ChatGPT when cellular data is unavailable.

OpenAI has been rolling out a series of exciting updates and features for ChatGPT, and…

3 days ago

Phishers use fake Google Calendar invites to target victims

A financially motivated phishing campaign has targeted around 300 organizations, with over 4,000 spoofed emails…

4 days ago

Hackers Exploiting Microsoft Teams to Remotely Access Users’ Systems

Hackers are exploiting Microsoft Teams to deceive users into installing remote access tools, granting attackers…

5 days ago

Ethical Hacking Essentials

Data plays an essential role in our lives.  We each consume and produce huge amounts…

6 days ago

Thomas E. Kurtz, co-creator of the BASIC programming language, passes away at 96.

Thomas E. Kurtz, co-creator of the BASIC programming language, passed away on November 12, 2024,…

6 days ago