What is getDate()?
The getDate() method is a built-in JavaScript function that extracts the day of the month from a Date object. Part of the JavaScript Date prototype, this method returns an integer value between 1 and 31, representing the day of the month according to local time.
The method operates on Date objects, which are created using the Date constructor. Whether you're building a calendar component, showing a publish date for blog posts, or calculating due dates, extracting the day of the month is fundamental to any application that works with temporal data. For SEO-friendly date displays, proper date formatting helps search engines understand your content freshness.
As a core part of the JavaScript Date API, getDate() provides a straightforward way to access this common data point without requiring external libraries. Understanding this method forms the foundation for more complex date manipulation in your web applications.
Syntax and Parameters
Basic Syntax
dateObject.getDate()
The syntax is intentionally simple. The getDate() method takes no parameters--it operates solely on the Date object it's called from. You simply invoke it on any valid Date instance, and it returns the day of the month as a number between 1 and 31.
Return Value
- Valid dates: Integer between 1 and 31
- Invalid dates: NaN (Not a Number)
When called on a valid Date object, getDate() returns the day of the month without any padding. For example, the third day returns 3, not "03". This behavior is consistent across all JavaScript environments, from browsers to Node.js servers. Combined with other Date methods like getMonth(), getFullYear(), and getDay(), you can build comprehensive date handling utilities for your applications.
Practical Code Examples
Getting Today's Day
const today = new Date();
const currentDay = today.getDate();
console.log(`Today is day ${currentDay} of the month`);
// Output: Today is day 3 of the month
The most common use case is retrieving the current day of the month. When you create a Date object without arguments, it defaults to the current date and time. Calling getDate() on this instance immediately gives you today's day value.
Extracting Day from Specific Dates
const christmas = new Date('2025-12-25');
const christmasDay = christmas.getDate();
console.log(`Christmas falls on day ${christmasDay}`);
// Output: Christmas falls on day 25
You can create Date objects from specific date strings and extract their day values. This pattern is essential for building date-based features like anniversary reminders, deadline calculators, and event calendars. When handling date inputs from users, consider combining getDate() with input validation techniques for robust form handling.
Working with Date Components
function getFullDateInfo(dateString) {
const date = new Date(dateString);
return {
day: date.getDate(),
month: date.getMonth() + 1,
year: date.getFullYear(),
dayOfWeek: date.getDay(),
isWeekend: date.getDay() === 0 || date.getDay() === 6
};
}
const info = getFullDateInfo('2025-01-15');
console.log(info);
// Output: { day: 15, month: 1, year: 2025, dayOfWeek: 3, isWeekend: false }
For comprehensive date handling, you'll often combine getDate() with other Date methods like getMonth(), getFullYear(), and getDay(). Note that getMonth() returns months as 0-indexed values (0 for January, 11 for December), so adding 1 gives you the conventional 1-12 numbering. This approach mirrors how response headers handle metadata--extracting specific components from a larger data structure.
Validate Before Use
Always check if a Date is valid using isNaN(date.getTime()) before calling getDate() to prevent unexpected NaN values in your application.
Cache Date Objects
Reuse Date objects instead of creating new ones repeatedly for better performance in date-intensive operations and React rendering cycles.
Consider Time Zones
getDate() uses local time. Use getUTCDate() for time zone-independent operations across users in different regions.
Handle Month Index Offset
Remember getMonth() returns 0-indexed values--add 1 to get conventional month numbering and avoid off-by-one errors.
Common Pitfalls and Solutions
The Month Index Gotcha
// getMonth() returns 0 for January, 11 for December
const januaryDate = new Date('2025-01-15');
console.log(januaryDate.getMonth()); // 0, not 1!
// Correct approach
const month = januaryDate.getMonth() + 1; // Now correctly 1
A frequent source of bugs is the 0-indexed nature of getMonth(). Always remember that January returns 0, not 1. This is similar to how JavaScript's array indexing starts at zero--a common pattern that developers should internalize.
Invalid Date Strings
// These create invalid dates
const invalid = new Date('not-a-date');
console.log(isNaN(invalid.getTime())); // true
console.log(invalid.getDate()); // NaN
Date parsing is forgiving--it will normalize some invalid dates and reject others entirely. Always validate using getTime() before relying on getDate() results. For robust date handling in production applications, consider using established date libraries that provide consistent parsing behavior across different input formats.
Date Object Mutability
const date1 = new Date('2025-01-15');
const date2 = date1; // Both reference the same object
date2.setDate(20);
console.log(date1.getDate()); // 20 - unexpected if unchanged!
// Create independent copy
const date3 = new Date(date1.getTime());
date3.setDate(25);
console.log(date1.getDate()); // 20 - unchanged
Date objects are mutable, which can lead to unexpected behavior when passing dates between functions. This mutability pattern is important to understand--similar considerations apply when working with DOM manipulation where objects are shared by reference.
| Method | Returns | Example |
|---|---|---|
| getDate() | Day of month (1-31) | 15 |
| getDay() | Day of week (0-6) | 3 (Wednesday) |
| getMonth() | Month (0-11) | 0 (January) |
| getFullYear() | 4-digit year | 2025 |
| getHours() | Hours (0-23) | 14 |
| getMinutes() | Minutes (0-59) | 30 |
When to Use Libraries
For simple day extraction like getDate(), the native method is typically sufficient. However, consider date libraries for:
- Complex date calculations (date ranges, recurrences)
- Time zone conversions and Daylight Saving Time handling
- Localization and internationalization for global applications
- Parsing various date formats consistently
- Validation with detailed error messages
Popular options include date-fns (modular, tree-shakeable) and Day.js (lightweight, API-compatible with moment.js). For Next.js applications, these libraries integrate well with server-side rendering. When building AI-powered applications that process temporal data, robust date handling becomes critical for accurate predictions and scheduling.
While modern libraries offer sophisticated date manipulation, the native Date methods remain valuable for simple operations. They add no bundle size, require no dependencies, and perform efficiently for basic use cases like extracting the day of the month. Understanding these fundamentals helps you make informed decisions about when to leverage external libraries versus native JavaScript.
Frequently Asked Questions
What does getDate() return for invalid dates?
getDate() returns NaN (Not a Number) for invalid dates. Always validate dates using isNaN(date.getTime()) before using getDate() to prevent unexpected behavior.
What's the difference between getDate() and getDay()?
getDate() returns the day of the month (1-31), while getDay() returns the day of the week (0-6, where 0 is Sunday). Use getDate() for calendar dates, getDay() for determining weekdays.
How do I get the day in a specific time zone?
Use getUTCDate() for UTC time, or use a library like date-fns-tz with the Intl API for arbitrary time zones. This is crucial for server-side applications.
Does getDate() consider daylight saving time?
No, getDate() simply returns the calendar day. DST affects time-of-day methods like getHours(), not getDate().
Sources
- MDN Web Docs - Date.prototype.getDate() - Official JavaScript reference documentation maintained by Mozilla.
- W3Schools - JavaScript Date getDate() Method - Widely-used web development tutorial site with practical examples.
- GeeksforGeeks - JavaScript Date getDate() Method - Computer science education platform with in-depth examples.