Javascript
  • BeThatCoder -
  • 2025-02-25

Working with Moment.js: A Comprehensive Guide

Moment.js is a powerful and widely-used JavaScript library that simplifies working with dates and times. It provides a wide range of methods for parsing, formatting, comparing, and manipulating dates, making it ideal for complex date operations. Although Moment.js is in maintenance mode, it remains a reliable tool for many applications.

Getting Started with Moment.js

Installation

You can easily install Moment.js using npm or a CDN:

Using npm:


                                npm install moment
                            

Using a CDN: Add the following script to your HTML file:


                                <script src=""></script>
                            

Creating Dates in Moment.js

Moment.js offers a straightforward way to create dates in various formats:

Current Date and Time


                                const now = moment();
console.log(now.format()); // Logs current date and time in ISO 8601 format
                            

Specific Date


                                const specificDate = moment('2025-01-23');
console.log(specificDate.format('YYYY-MM-DD')); // 2025-01-23
                            

Parsing Dates Moment.js can handle different date formats:


                                const parsedDate = moment('23/01/2025', 'DD/MM/YYYY');
console.log(parsedDate.format('YYYY-MM-DD')); // 2025-01-23
                            

Formatting Dates

Formatting dates in Moment.js is simple and flexible using the .format() method. You can use custom patterns to format your dates in a variety of ways:


                                const date = moment('2025-01-23');
console.log(date.format('YYYY-MM-DD')); // 2025-01-23
console.log(date.format('MMMM Do, YYYY')); // January 23rd, 2025
console.log(date.format('dddd, MMMM Do YYYY, h:mm:ss a')); // Thursday, January 23rd, 2025, 12:00:00 am
                            

Manipulating Dates

Moment.js provides several methods to add, subtract, and set date components easily:

Adding Time


                                const date = moment('2025-01-23');
console.log(date.add(7, 'days').format('YYYY-MM-DD')); // 2025-01-30
console.log(date.add(1, 'month').format('YYYY-MM-DD')); // 2025-02-23
                            

Subtracting Time


                                const date = moment('2025-01-23');
console.log(date.subtract(10, 'days').format('YYYY-MM-DD')); // 2025-01-13
                            

Setting Time

You can set specific date components:


                                const date = moment('2025-01-23');
console.log(date.year(2030).format('YYYY-MM-DD')); // 2030-01-23
console.log(date.month(5).format('YYYY-MM-DD')); // 2030-06-23
                            

Comparing Dates

Moment.js offers methods to compare dates easily:


                                const date1 = moment('2025-01-23');
const date2 = moment('2025-02-01');

console.log(date1.isBefore(date2)); // true
console.log(date1.isAfter(date2)); // false
console.log(date1.isSame('2025-01-23', 'day')); // true
                            

Calculating Date Differences

To calculate the difference between two dates, you can use the .diff() method:


                                const start = moment('2025-01-01');
const end = moment('2025-01-23');

console.log(end.diff(start, 'days')); // 22 days
console.log(end.diff(start, 'weeks')); // 3 weeks
                            

Displaying Relative Time

Moment.js can display human-readable relative time:


                                const date = moment().subtract(3, 'days');
console.log(date.fromNow()); // 3 days ago

const futureDate = moment().add(5, 'days');
console.log(futureDate.fromNow()); // in 5 days
                            

Conclusion

Moment.js remains a robust library for handling date and time manipulation in JavaScript, but with its shift to maintenance mode, it's wise to explore newer alternatives like date-fns or Luxon for future-proof solutions. Whether you are formatting dates, manipulating them, or handling time zones, Moment.js offers an easy-to-use API for complex operations.