In JavaScript, we can get the current date using the following methods:
Date().toLocaleDateString()
.Date().toLocaleDateString()
The Date().toLocaleDateString()
method is used to return only the date portion of the Date()
object. The format of the date is mm/dd/yyyy.
Date().toLocaleDateString();
This method doesn't accept any parameters.
This method returns the current date.
Let's see an example of the Date().toLocaleDateString()
method in the code snippet below:
// create a Date objectconst date = new Date();// get the current date from Date objectconst currDate = date.toLocaleDateString();// print on consoleconsole.log(currDate);
Date
object.Date
object.Note: To get the current date in yyyy-mm-dd format, we can use theDate().toISOString()
method. It is very similar to theDate().toLocaleDateString()
method.
We can get the current date of the month, current month of the year, and current year using the getDate()
, getMonth()
, and the getFullYear()
method respectively on the Date()
object.
We can then combine them together to get the current date.
Let's see an example to get the current date using individual methods in the code snippet below:
// create a Date objectconst date = new Date();// get the current date of the monthconst currDate = date.getDate();// get the current month of the yearconst currMonth = date.getMonth() + 1;// get the current yearconst currYear = date.getFullYear();// combine above fields to get current dateconst output = `${currMonth}/${currDate}/${currYear}`;// print on consoleconsole.log(output);
Date
object.getDate()
method.getMonth()
method.getFullYear()
method.