How to use toLocaleDateString() Method In Javascript

  • In this method, we are using the toLocalDateString() function with the options like { day: ‘numeric’, month: ‘numeric’, year: ‘numeric’ }.
  • These are used to format the date as a string and split the formatted string into proper day, month, and year formats.

Example: In this example, we will be extracting Day, Month, and Year from the Date object in JavaScript using toLocaleDateString() method.

Javascript




let obj = new Date();
let temp = { day: 'numeric', month: 'numeric', year: 'numeric' };
let dateFormat = obj.toLocaleDateString(undefined, temp);
let [month, day, year] = dateFormat.split('/'); 
console.log(`Day: ${day}, Month: ${month}, Year: ${year}`);


Output

Day: 26, Month: 11, Year: 2023


How to get Day Month and Year from Date Object in JavaScript ?

We have given the Date object and the task is to extract the Day, Month, and Year from the Date object and print it using JavaScript. Below is an example for better understanding.

Example:

Input: Date Object
Output: Day: 26 Month: 11 Year: 2023

To extract Day, Month, and Year from Date Object, we have three different methods which are stated below:

Table of Content

  • Using getUTCDate(), getUTCMonth(), and getUTCFullYear() Methods
  • Using getDate(), getMonth(), and getFullYear() Methods
  • Using toLocaleDateString() Method

Similar Reads

Method 1: Using getUTCDate(), getUTCMonth(), and getUTCFullYear() Methods

In this method, we are using built-in methods like getUTCDate() to get the day of the month. The getUTCMonth() method to extract the month (Adding 1 because months are zero-indexed). The getUTCFullYear() method to get the full year from the Date object in UTC format....

Method 2: Using getDate(), getMonth(), and getFullYear() Methods

...

Method 3: Using toLocaleDateString() Method

In this method, we have used local time methods like getDate() to retrieve the date of the month. The getMonth() method to get the month of the Date. The getFullYear() to get the full year from the Date object. This method provides the local time zone for the user to extract the day, month, and year from the Date object....

Contact Us