Date.prototype.getDate()
Return the day of the month for a date according to local time.
getDate()- Added in JavaScript ES1
Return value
numberAn integer between 1 and 31 representing the day of the month for the given date according to local time. Returns NaN if the date is invalid.
Errors
Throws a TypeError if called on a value that is not a Date object.
Returns the day of the month — the number you would read off a wall calendar — in local time. It is 1-based, unlike getMonth(), which is the inconsistency at the root of most confusion here.
getDate() or getDay()
The names are close enough that they get swapped constantly. getDate() answers which date in the month and returns 1 to 31. getDay() answers which weekday and returns 0 to 6. If your output is unexpectedly small and never exceeds 6, you called the wrong one.
When to reach for it
Use it to format a date by hand, to check for a monthly boundary such as the 1st or the last day, to compare two dates for the same calendar day, or to shift a date with setDate().
What trips people up
setDate() is the cleanest way to add or subtract days, because it normalizes across month and year boundaries: d.setDate(d.getDate() + 30) from 1 June lands on 1 July with no arithmetic on your part. It also accepts 0 and negative values, walking backwards into the previous month — new Date(y, m + 1, 0).getDate() is the idiomatic way to get the number of days in a month.
Comparing two Dates for the same day requires checking year, month and date together; comparing getDate() alone matches the 15th of every month.
Example
When to use it
- Format a date manually as DD/MM/YYYY without a library
- Detect the first or last day of the month for billing logic
- Add or subtract days from a date and let the month roll over correctly
- Check whether two timestamps fall on the same calendar day
Frequently asked questions
What is the difference between getDate() and getDay()?
getDate() returns the day of the month from 1 to 31, while getDay() returns the day of the week from 0 (Sunday) to 6 (Saturday). If your value never rises above 6, you called getDay() by mistake.
How do I add days to a date in JavaScript?
Copy the date, then call copy.setDate(copy.getDate() + n) — setDate() normalizes across month and year boundaries automatically, including leap days. Copy first, because setDate() mutates the object.
How do I get the number of days in a month?
Use new Date(year, monthIndex + 1, 0).getDate(), which asks for day 0 of the following month and lands on the last day of the one you want. This handles leap years without a special case.
More examples
Adding days safely
setDate() normalizes past month and year boundaries, so 29 February appears in 2024 but 2023 jumps straight to 1 March. Negative counts walk backwards into December.
function addDays(date, n) {
const copy = new Date(date);
copy.setDate(copy.getDate() + n);
return copy;
}
console.log(addDays(new Date(2024, 0, 25), 10).toDateString());
console.log(addDays(new Date(2024, 1, 28), 1).toDateString());
console.log(addDays(new Date(2023, 1, 28), 1).toDateString());
console.log(addDays(new Date(2024, 0, 5), -10).toDateString());Same calendar day?
All three fields are needed. The final line shows why getDate() alone fails: 10 June and 10 July both report 10.
function isSameDay(a, b) {
return a.getFullYear() === b.getFullYear()
&& a.getMonth() === b.getMonth()
&& a.getDate() === b.getDate();
}
const morning = new Date(2024, 5, 10, 8, 0);
const evening = new Date(2024, 5, 10, 23, 59);
const nextDay = new Date(2024, 5, 11, 0, 1);
console.log(isSameDay(morning, evening));
console.log(isSameDay(morning, nextDay));
console.log(morning.getDate() === new Date(2024, 6, 10).getDate());Days left in the month
Day 0 of March gives 29 for the leap February, so 19 days remain after the 10th. On the last day of a month the answer is 0.
function daysRemaining(date) {
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
return lastDay - date.getDate();
}
console.log(daysRemaining(new Date(2024, 1, 10))); // February, leap year
console.log(daysRemaining(new Date(2024, 3, 30))); // last day of April
console.log(new Date(2024, 1, 10).getDate());
console.log(new Date(2024, 2, 0).getDate());
Discussion