How to use Date object with toLocaleDateString() Method In Javascript
The date object comes with a method known as toLocaleDateString() with parameters such as a locale which represents the location, of which the date format should be shown, and a list of options to indicate the format of how the year, month and the day of the date should be represented.
Example 1: The below code uses the Date object with toLocaleDateString() method with a specified locale (en-GB) and a list of options to represent the format of the year, the month and the day to log the current date in JavaScript.
Javascript
const currentDate = new Date().toLocaleDateString( "en-GB" , { "year" : "numeric" , "month" : "numeric" , "day" : "numeric" }); console.log(currentDate); |
23/01/2024
Example 2: The below code uses the Date object with toLocaleDateString() method with a specified locale (en-IN) and a list of options to represent the format of the year, the month and the day to log the current date in JavaScript.
Javascript
const currentDate = new Date().toLocaleDateString( "en-IN" , { "year" : "numeric" , "month" : "short" , "day" : "numeric" }); console.log(currentDate); |
23 Jan 2024
How to Log the Current Date in JavaScript ?
The Date object is an inbuilt object constructor of JavaScript language. It is used to work with dates and times. The Date object is created by using the new keyword, i.e. new Date(). The current date can be logged on the console in JavaScript using the below-discussed methods.
Table of Content
- Using JavaScript Date object
- Using JavaScript Date object with toString() Method
- Using JavaScript Date object with toDateString() Method
- Using JavaScript Date object with toLocaleDateString() Method
- Using JavaScript Date object with toISOString() Method