JavaScript Date Objects
JavaScript Date objects provide functionality for working with dates and times. Internally, dates are stored as the number of milliseconds since January 1, 1970 00:00:00 UTC (the Unix epoch), making date arithmetic and comparisons straightforward.
Creating Dates
The new Date() constructor with no arguments creates a Date object representing the current date and time at the moment of creation. This is the most common way to get the current timestamp. The Date object contains both date and time information down to the millisecond, though you can extract just the parts you need with various methods.
The new Date(milliseconds) constructor creates a Date object from a timestamp—the number of milliseconds since January 1, 1970 UTC. new Date(0) represents that epoch moment. Timestamps are useful for date arithmetic and storage because they're just numbers. You can get a timestamp from a Date with date.getTime() or Date.now() for the current time.
The new Date(dateString) constructor parses a date string and creates a Date object. JavaScript can parse various formats like "2025-01-01" (ISO format), "January 1, 2025", or "01/01/2025". However, date string parsing can be inconsistent across browsers and locales. For reliability, use ISO 8601 format (YYYY-MM-DD) or create dates from individual components.
The new Date(year, month, ...) constructor creates a Date from individual components: year, month (0-11), day (1-31), hours (0-23), minutes (0-59), seconds (0-59), milliseconds (0-999). You must provide at least year and month. This is the most reliable way to create specific dates because it's unambiguous and works consistently across all environments.
A critical gotcha: month numbers are 0-indexed, meaning January is 0, February is 1, through December which is 11. This is a common source of bugs. When you write new Date(2025, 0, 1), you're creating January 1, 2025, not an error. Day of month, hours, minutes, and seconds use normal 1-based or 0-based counting as you'd expect, but months are the exception.
// Current date and time
const now = new Date();
console.log(now);
// From timestamp
const date1 = new Date(0); // Jan 1, 1970
const date2 = new Date(1000000000000);
// From string
const date3 = new Date("2025-01-01");
const date4 = new Date("January 1, 2025");
// From parameters (year, month, day, hour, minute, second)
const date5 = new Date(2025, 0, 1); // Jan 1, 2025
const date6 = new Date(2025, 0, 1, 12, 30, 0);
Date Methods
The getFullYear() method returns the four-digit year of a Date object. Always use getFullYear(), never the deprecated getYear() which returns a two-digit year offset. For example, new Date("2025-01-01").getFullYear() returns 2025. This method is essential for displaying years or performing year-based logic like age calculations or year comparisons.
The getMonth() method returns the month as a number from 0-11, where 0 represents January and 11 represents December. Remember to add 1 when displaying months to users (getMonth() + 1) since people think of months as 1-12. This zero-indexing is consistent with the Date constructor but often trips up developers.
The getDate() method returns the day of the month as a number from 1-31. Note the confusing naming: getDate() returns the day of month, not the full date. For the full date information, you'd use multiple methods. getDate() is useful for calendar displays, date arithmetic, or checking specific days.
The getDay() method returns the day of the week as a number from 0-6, where 0 is Sunday, 1 is Monday, through 6 which is Saturday. This doesn't return a day name—you'd need an array like ["Sunday", "Monday", ...] to convert the number to a name. getDay() is useful for weekend detection, scheduling, or calendar applications.
Time-related methods include getHours() (0-23), getMinutes() (0-59), getSeconds() (0-59), and getMilliseconds() (0-999). These extract time components from a Date object. All use 24-hour format. For 12-hour format with AM/PM, you need to convert manually: (hours % 12 || 12) + (hours >= 12 ? ' PM' : ' AM').
The toDateString() method converts a Date to a human-readable string containing just the date portion (no time), like "Wed Jan 01 2025". Related methods include toTimeString() (time portion), toLocaleDateString() (locale-specific date), and toISOString() (ISO 8601 format). These formatting methods make dates presentable without manual string construction.
const date = new Date();
// Get date components
console.log(date.getFullYear()); // 2025
console.log(date.getMonth()); // 0-11
console.log(date.getDate()); // 1-31
console.log(date.getDay()); // 0-6 (Sunday-Saturday)
console.log(date.getHours()); // 0-23
console.log(date.getMinutes()); // 0-59
// Format date
console.log(date.toDateString()); // "Wed Jan 01 2025"
console.log(date.toTimeString()); // "12:30:00 GMT+0000"
console.log(date.toLocaleDateString()); // Locale-specific