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
Contact Us