The getDate()
method in JavaScript is used to get the day of the month for the specified date according to local time. The day of the month is represented as an integer between 1 and 31.
Here is an example of how to use the getDate()
method:
const date = new Date();
const dayOfMonth = date.getDate();
console.log(dayOfMonth); // output: 18 (assuming today is December 18)
You can also pass a date as an argument to the Date
constructor to get the day of the month for a specific date:
const date = new Date('January 1, 2021');
const dayOfMonth = date.getDate();
console.log(dayOfMonth); // output: 1
Note that the getDate()
method returns the day of the month in the local timezone. If you want to get the day of the month in a specific timezone, you can use the getUTCDate()
method instead.
For example:
const date = new Date();
const utcDayOfMonth = date.getUTCDate();
console.log(utcDayOfMonth); // output: 18
I hope this helps! Let me know if you have any questions.
Leave a Reply