How do you format a Date object in JavaScript?

Formatting a date is a common task in JavaScript development, as it allows you to display a date in a specific format according to your needs.

In this article, we’ll explore how to format a Date object in JavaScript using various techniques and code snippets.

Before we dive into the code, let’s take a quick look at the Date object in JavaScript.

The Date object is a built-in object in JavaScript that represents a date and time. It provides a wide range of methods for working with dates, such as getting the current date and time, setting the date and time, and manipulating dates. Here’s an example of creating a Date object:

const today = new Date();

This creates a new Date object that represents the current date and time.

Now, let’s explore some common ways to format a date in JavaScript.

Formatting a Date using toLocaleDateString():

The toLocaleDateString() method is a built-in method in JavaScript that returns a string that represents the date in the local time zone using the browser’s locale settings.

Here’s an example:

const today = new Date();
const formattedDate = today.toLocaleDateString();
console.log(formattedDate); // Output: 5/9/2023

In this example, we created a new Date object and used the toLocaleDateString() method to format the date. The method returns a string that represents the date in the local time zone using the default format.

You can also pass options to the toLocaleDateString() method to customize the format of the date.

Here’s an example:

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
const today = new Date();
const formattedDate = today.toLocaleDateString(undefined, options);
console.log(formattedDate); // Output: Monday, May 9, 2023

In this example, we created an options object with properties for the weekday, year, month, and day. We then passed the options object as the second argument to the toLocaleDateString() method to format the date. The method returns a string that represents the date in the local time zone using the specified format.

Formatting a Date using toLocaleTimeString():

The toLocaleTimeString() method is a built-in method in JavaScript that returns a string that represents the time in the local time zone using the browser’s locale settings.

Here’s an example:

const today = new Date();
const formattedTime = today.toLocaleTimeString();
console.log(formattedTime); // Output: 3:35:08 PM

In this example, we created a new Date object and used the toLocaleTimeString() method to format the time. The method returns a string that represents the time in the local time zone using the default format.

You can also pass options to the toLocaleTimeString() method to customize the format of the time.

Here’s an example:

const options = { hour: "numeric", minute: "numeric", second: "numeric" };
const today = new Date();
const formattedTime = today.toLocaleTimeString(undefined, options);
console.log(formattedTime); // Output: 3:35:08 PM

In this example, we created an options object with properties for the hour, minute, and second. We then passed the options object as the second argument to the toLocaleTimeString() method to format the time. The method returns a string that represents the time in the local time zone using the specified format.

Formatting a Date using toLocaleString():

The toLocaleString() method is a built-in method in JavaScript that returns a string that represents the date and time in the local time zone using the browser’s locale settings.

Here’s an example:

const today = new Date();
const formattedDateTime = today.toLocaleString();
console.log(formattedDateTime); // Output: 5/9/2023, 3:35:08 PM

In this example, we created a new Date object and used the toLocaleString() method to format the date and time. The method returns a string that represents the date and time in the local time zone using the default format.

You can also pass options to the toLocaleString() method to customize the format of the date and time.

Here’s an example:

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "numeric",
  minute: "numeric",
  second: "numeric",
};
const today = new Date();
const formattedDateTime = today.toLocaleString(undefined, options);
console.log(formattedDateTime); // Output: Monday, May 9, 2023, 3:35:08 PM

In this example, we created an options object with properties for the weekday, year, month, day, hour, minute, and second. We then passed the options object as the second argument to the toLocaleString() method to format the date and time. The method returns a string that represents the date and time in the local time zone using the specified format.

Formatting a Date using Intl.DateTimeFormat():

The Intl.DateTimeFormat() constructor is a built-in constructor in JavaScript that provides a way to format dates and times according to the language and cultural conventions of a specific locale.

Here’s an example:

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "numeric",
  minute: "numeric",
  second: "numeric",
};
const today = new Date();
const formatter = new Intl.DateTimeFormat(undefined, options);
const formattedDateTime = formatter.format(today);
console.log(formattedDateTime); // Output: Monday, May 9, 2023, 3:35:08 PM

In this example, we created an options object with properties for the weekday, year, month, day, hour, minute, and second. We then created a new DateTimeFormat object with the options object and passed undefined as the first argument to use the browser’s locale settings. We then used the format() method of the formatter object to format the date and time. The method returns a string that represents the date and time using the specified format.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: