Date .getFullYear() / getMonth() / getDate()
Methods for retrieving individual components — such as year, month, day, and day of the week — from a Date object. Use these to build calendars and date displays.
Syntax
// Returns the year as a 4-digit number. var year = date.getFullYear(); // Returns the month. Zero-based: January is 0, December is 11. var month = date.getMonth(); // Returns the day of the month. var day = date.getDate(); // Returns the day of the week. 0 is Sunday, 6 is Saturday. var weekday = date.getDay(); // Returns the hours, minutes, seconds, and milliseconds. var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); var ms = date.getMilliseconds(); // Returns the timestamp. var timestamp = date.getTime();
Method List
| Method | Description |
|---|---|
| date.getFullYear() | Returns the year as a 4-digit number. |
| date.getMonth() | Returns the month as a number from 0 to 11. January is 0, December is 11. |
| date.getDate() | Returns the day of the month as a number from 1 to 31. |
| date.getDay() | Returns the day of the week as a number from 0 to 6. Sunday is 0, Saturday is 6. |
| date.getHours() | Returns the hour as a number from 0 to 23. |
| date.getMinutes() | Returns the minutes as a number from 0 to 59. |
| date.getSeconds() | Returns the seconds as a number from 0 to 59. |
| date.getMilliseconds() | Returns the milliseconds as a number from 0 to 999. |
| date.getTime() | Returns the number of milliseconds elapsed since January 1, 1970. |
Sample Code
var date = new Date(2025, 3, 15, 14, 30, 45); // April 15, 2025, 14:30:45
// Retrieve each component.
console.log(date.getFullYear()); // Outputs 2025.
console.log(date.getMonth()); // Outputs 3. April is 3 because months are zero-based.
console.log(date.getDate()); // Outputs 15.
console.log(date.getDay()); // Outputs 2. Tuesday.
console.log(date.getHours()); // Outputs 14.
console.log(date.getMinutes()); // Outputs 30.
console.log(date.getSeconds()); // Outputs 45.
// Practical example: format a date as "YYYY/MM/DD"
var now = new Date();
var y = now.getFullYear();
var m = now.getMonth() + 1; // Add 1 because months are zero-based.
var d = now.getDate();
console.log(y + "/" + m + "/" + d);
// Practical example: display the day of the week as a name
var weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var dayName = weekdays[now.getDay()];
console.log(y + "/" + m + "/" + d + " (" + dayName + ")");
// Practical example: zero-pad a number to two digits
function zeroPad(num) {
return ("0" + num).slice(-2);
}
var timeStr = zeroPad(now.getHours()) + ":" + zeroPad(now.getMinutes());
console.log(timeStr); // Outputs a time like "09:05" with leading zeros.
Overview
The Date getter methods retrieve each component of a date and time as a number. They are useful whenever you need to break a date into individual parts, such as when building a calendar or formatting a date string.
The most common pitfall is that date.getMonth() is zero-based. Always add 1 when displaying the month to users. Similarly, date.getDay() is zero-based, where 0 is Sunday and 6 is Saturday. To display a day name, the typical approach is to create an array of names and use the returned number as an index, as shown in the sample code.
When formatting a date as a string, you can either combine these getter methods yourself, or use date.toLocaleDateString() to format the date according to a locale.
Browser Compatibility
3 or earlier ×
3 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.