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.
// 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");
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
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);
moment().add(7, 'days');
moment().add(1, 'months');
moment().add(1, 'years');
moment().add({days: 7, months: 1, years: 1});
moment().subtract(7, 'days');
moment().subtract(1, 'months');
moment().subtract(1, 'years');
moment().subtract({days: 7, months: 1, years: 1});
moment().startOf('day');
moment().startOf('month');
moment().startOf('year');
moment().startOf('week');
moment().endOf('day');
moment().endOf('month');
moment().endOf('year');
moment().endOf('week');
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
moment("20111031", "YYYYMMDD").fromNow();
// => "12 years ago"
moment().startOf('day').fromNow();
// => "16 hours ago"
const a = moment([2023, 0, 1]);
const b = moment([2024, 0, 1]);
a.diff(b, 'days'); // -365
moment('2010-10-20').isBefore('2010-10-21'); // true
moment('2010-10-20').isAfter('2010-10-19'); // true
moment('2010-10-20').isSame('2010-10-20'); // true
moment('2010-10-20').isSame('2010-10-21', 'month'); // true
moment([2000]).isLeapYear(); // true
moment([2001]).isLeapYear(); // false
moment([2023, 5, 1]).isDST(); // true for many timezones
moment.duration(1, 'days');
moment.duration(2, 'weeks');
moment.duration({
seconds: 2,
minutes: 2,
hours: 2,
days: 2,
weeks: 2,
months: 2,
years: 2
});
moment.duration(1, "minutes").humanize(); // a minute
moment.duration(24, "hours").humanize(); // a day
moment().unix(); // Seconds since the Unix Epoch
moment("2012-02", "YYYY-MM").daysInMonth(); // 29
moment([2023, 5, 1]).isDST(); // true for many timezones
moment.locale('fr');
moment(1316116057189).fromNow(); // il y a 12 ans
moment.locale('es');
moment(1316116057189).fromNow(); // hace 12 años
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