logo
eng-flag

Moment.js Cheatsheet

Moment.js is a powerful library for parsing, validating, manipulating, and formatting dates in JavaScript. This cheatsheet provides an overview of commonly used Moment.js functions with examples.

Table of Contents

  1. Parsing
  2. Get and Set
  3. Manipulating
  4. Displaying
  5. Query
  6. Duration
  7. Utilities

Parsing

Creating Moment Objects

// Current date and time
moment();

// From ISO 8601 string
moment("2023-03-15T12:00:00");

// From Unix Timestamp (milliseconds)
moment(1678896000000);

// From Date object
moment(new Date(2023, 2, 15));

// Specific format
moment("15/03/2023", "DD/MM/YYYY");

// Parse UTC
moment.utc("2023-03-15T12:00:00Z");

Get and Set

Getters

const now = moment();

now.year();       // Get the year
now.month();      // Get the month (0-11)
now.date();       // Get the day of the month
now.day();        // Get the day of the week (0-6)
now.hour();       // Get the hour
now.minute();     // Get the minute
now.second();     // Get the second
now.millisecond();// Get the millisecond

Setters

const date = moment();

date.year(2024);
date.month(0);    // January
date.date(1);
date.hour(12);
date.minute(30);
date.second(0);
date.millisecond(0);

// Chaining
date.year(2024).month(0).date(1).hour(12).minute(30).second(0).millisecond(0);

Manipulating

Add Time

moment().add(7, 'days');
moment().add(1, 'months');
moment().add(1, 'years');
moment().add({days: 7, months: 1, years: 1});

Subtract Time

moment().subtract(7, 'days');
moment().subtract(1, 'months');
moment().subtract(1, 'years');
moment().subtract({days: 7, months: 1, years: 1});

Start of Time

moment().startOf('day');
moment().startOf('month');
moment().startOf('year');
moment().startOf('week');

End of Time

moment().endOf('day');
moment().endOf('month');
moment().endOf('year');
moment().endOf('week');

Displaying

Formatting

moment().format('YYYY-MM-DD');
moment().format('DD/MM/YYYY');
moment().format('MMMM Do YYYY, h:mm:ss a');
moment().format('dddd');
moment().format('LT');   // 8:30 PM
moment().format('LTS');  // 8:30:25 PM
moment().format('L');    // 09/04/2023
moment().format('LL');   // September 4, 2023
moment().format('LLL');  // September 4, 2023 8:30 PM
moment().format('LLLL'); // Monday, September 4, 2023 8:30 PM

Time from Now

moment("20111031", "YYYYMMDD").fromNow();
// => "12 years ago"

moment().startOf('day').fromNow();
// => "16 hours ago"

Time Between Two Dates

const a = moment([2023, 0, 1]);
const b = moment([2024, 0, 1]);
a.diff(b, 'days'); // -365

Query

Is Before

moment('2010-10-20').isBefore('2010-10-21'); // true

Is After

moment('2010-10-20').isAfter('2010-10-19'); // true

Is Same

moment('2010-10-20').isSame('2010-10-20'); // true
moment('2010-10-20').isSame('2010-10-21', 'month'); // true

Is Leap Year

moment([2000]).isLeapYear(); // true
moment([2001]).isLeapYear(); // false

Is DST

moment([2023, 5, 1]).isDST(); // true for many timezones

Duration

Creating Durations

moment.duration(1, 'days');
moment.duration(2, 'weeks');
moment.duration({
    seconds: 2,
    minutes: 2,
    hours: 2,
    days: 2,
    weeks: 2,
    months: 2,
    years: 2
});

Humanize Duration

moment.duration(1, "minutes").humanize(); // a minute
moment.duration(24, "hours").humanize(); // a day

Utilities

Unix Timestamp

moment().unix(); // Seconds since the Unix Epoch

Days in Month

moment("2012-02", "YYYY-MM").daysInMonth(); // 29

Is Daylight Saving Time

moment([2023, 5, 1]).isDST(); // true for many timezones

Locale

moment.locale('fr');
moment(1316116057189).fromNow(); // il y a 12 ans

moment.locale('es');
moment(1316116057189).fromNow(); // hace 12 años

Is Valid

moment("2023-01-01").isValid(); // true
moment("not-a-date").isValid(); // false

Note: The project team recommends using native JavaScript Date objects, or other modern libraries like date-fns or Luxon for new projects. However, Moment.js is still widely used in existing projects.

2024 © All rights reserved - buraxta.com