Formatting a moment object into a string

Working with dates and times in JavaScript is made easier with the help of libraries like Moment.js. Moment.js provides a simple and intuitive API for parsing, manipulating, and formatting dates and times.

In this blog post, we will explore how to format a Moment object into a string using the various formatting options provided by Moment.js.

Table of Contents

  1. Introduction
  2. Formatting
  3. Examples
  4. Conclusion

Introduction

Moment.js provides the format() function to format a Moment object into a string representation. This function takes a format string as an argument and returns the formatted string.

Formatting

The format string consists of tokens that are replaced with corresponding values from the Moment object. Here are some commonly used tokens:

For a complete list of formatting tokens, refer to the Moment.js documentation.

Examples

Let’s look at some examples to understand how to format a Moment object into a string:

const moment = require('moment');

const now = moment(); // current date and time

const formattedDate = now.format('YYYY-MM-DD'); // e.g., 2022-01-01
const formattedTime = now.format('HH:mm:ss'); // e.g., 13:45:30
const formattedDateTime = now.format('YYYY-MM-DD HH:mm:ss'); // e.g., 2022-01-01 13:45:30

console.log(formattedDate);
console.log(formattedTime);
console.log(formattedDateTime);

Conclusion

Formatting a Moment object into a string is straightforward with the help of Moment.js. By using the format() function and providing the desired format string, you can easily customize the output according to your needs.

Experiment with different format strings and explore the possibilities that Moment.js offers when it comes to working with dates and times in JavaScript.

#momentjs #dateformat