Converting between different date formats using Moment.js

When working with date and time in JavaScript, manipulating and formatting these values can be a tedious task. Luckily, there are several libraries available that make this process easier, and one of the most popular ones is Moment.js.

Moment.js is a lightweight JavaScript library that makes it easy to parse, manipulate, and format dates and times. In this blog post, we’ll explore how to convert dates between different formats using Moment.js.

Installation

Before we can start using Moment.js, we need to install it. You can include Moment.js in your project by adding the following script tag to your HTML file:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Alternatively, you can install Moment.js using npm or yarn:

npm install moment

Converting between different date formats

To convert a date from one format to another using Moment.js, we can make use of its parsing and formatting functions.

Parsing a date

The first step is to parse a date from the original format. Moment.js provides the moment() function to parse a date. We can pass the original date string and its format as parameters to this function. For example, if the original date format is “YYYY-MM-DD”, we can parse it using the following code:

const originalDate = "2022-01-01";
const parsedDate = moment(originalDate, "YYYY-MM-DD");

Formatting a date

Once we have the parsed date, we can format it into the desired format using the format() function. We need to pass the desired format as a parameter to this function. Moment.js supports a wide range of format options, including year, month, day, hour, minute, and second. Here’s an example of formatting a date to “DD/MM/YYYY”:

const formattedDate = parsedDate.format("DD/MM/YYYY");

Example

Let’s say we have a date string in the format “MM/DD/YYYY” and we want to convert it to the format “YYYY-MM-DD”. We can achieve this by using Moment.js as follows:

const originalDate = "01/01/2022";
const parsedDate = moment(originalDate, "MM/DD/YYYY");
const formattedDate = parsedDate.format("YYYY-MM-DD");

console.log(formattedDate); // Output: 2022-01-01

Conclusion

Converting between different date formats can be a common task when working with dates and times in JavaScript. Thanks to Moment.js, this process becomes much simpler and more intuitive. By using the moment() function to parse a date from the original format and the format() function to convert it into the desired format, we can easily perform these conversions.