getDate()
ES1+Returns the day of the month (1-31) for the specified date according to local time.
Syntax
date.getDate()Return Value
A number (1-31) representing the day of the month
Examples
const date = new Date('2024-03-15');
console.log(date.getDate()); 📌 When to Use
Use getDate() for calendar displays, deadline calculations, billing cycle logic, or extracting the day component for date formatting.
⚠️ Common Mistakes
Confusing getDate() (day of month) with getDay() (day of week).
Not accounting for different month lengths when calculating future dates.
✅ Best Practices
Use setDate() with getDate() for adding/subtracting days: date.setDate(date.getDate() + 7).
For last day of month, use: new Date(year, month + 1, 0).getDate().
⚡ Performance Notes
getDate() is a simple extraction. JavaScript automatically handles month overflow when using setDate() with values > 31 or < 1.
🌍 Real World Example
Subscription Billing Date Calculator
Calculate next billing date for monthly subscriptions, handling months with fewer days.
function getNextBillingDate(startDate, billingDay) {
const now = new Date();
const billing = new Date(now.getFullYear(), now.getMonth(), billingDay);
// If billing day has passed this month, move to next month
if (billing.getDate() < now.getDate() || billing < now) {
billing.setMonth(billing.getMonth() + 1);
}
// Handle months with fewer days (e.g., billing day 31 in February)
const lastDayOfMonth = new Date(billing.getFullYear(), billing.getMonth() + 1, 0).getDate();
if (billingDay > lastDayOfMonth) {
billing.setDate(lastDayOfMonth);
}
return {
nextBilling: billing.toLocaleDateString(),
daysUntilBilling: Math.ceil((billing - now) / (1000 * 60 * 60 * 24))
};
}
console.log(getNextBillingDate('2024-01-15', 15));
// { nextBilling: '3/15/2024', daysUntilBilling: 10 }