How to use the toLocaleString() method In Javascript

toLocaleString() method will work in the similar way as the toLocaleDateString() works. The only difference is that it also returns the time with the formatted string.

Syntax:

dateObj.toLocaleString();

Example: The below example uses the toLocaleString() method to format the date.

Javascript
const currentDate = new Date();
const formattedLocalDate = currentDate.toLocaleString();
const formattedInSpecifiedFormat = currentDate.toLocaleString("hi-IN");
console.log(formattedLocalDate);
console.log(formattedInSpecifiedFormat);

Output
12/29/2023, 9:39:27 AM
29/12/2023, 9:39:27 am

How to Format a Date in JavaScript ?

In this article, we will learn about the different ways of formatting a Date object into different date strings of different formats using JavaScript.

We will see the methods of formatting Date objects into various date strings with different formats using JavaScript. Whether you’re building a web application, manipulating data, or simply displaying dates, mastering these techniques will empower you to present dates in the format that best suits your needs. Let’s explore the versatility of date formatting in JavaScript.

The below list contains the different ways of formatting Date in JavaScript:

Table of Content

  • Method 1: Using the toDateString() method
  • Method 2: Using the toISOString() method
  • Method 3: Using the toLocaleDateString() method
  • Method 4: Using the toLocaleString() method
  • Method 5: Using the Intl.DateTimeFormat() object method
  • Method 6: Manually format date using Date methods

Similar Reads

Method 1: Using the toDateString() method

The toDateString() method formats the date object into a human-readable format as Day Month Date Year....

Method 2: Using the toISOString() method

The toISOString() method will format the data into the International Standards following the ISO 8601 format....

Method 3: Using the toLocaleDateString() method

toLocaleDateString() method will format the date part of the date object into the same format as acquired by your system or in the specified format....

Method 4: Using the toLocaleString() method

toLocaleString() method will work in the similar way as the toLocaleDateString() works. The only difference is that it also returns the time with the formatted string....

Method 5: Using the Intl.DateTimeFormat() object method

Intl.DateTimeFormat() object method is a powerful object method to format the date object. It formats the date into specified format and with the specified options for formatting the date and time....

Method 6: Manually format date using Date methods

In this approach, we will use the different date methods to get the day, date, month and year of the date object and then concat them to form a formatted date string....

Contact Us