The Date
object in JavaScript is used to work with dates and times. It provides various methods to create, manipulate, and format dates.
1. Creating a Date Object
You can create a Date
object using the new Date()
constructor in several ways:
1.1. Current Date and Time
let currentDate = new Date(); console.log(currentDate); // Output: Current date and time
1.2. Specific Date and Time
let specificDate = new Date('2025-01-19T10:20:30Z'); console.log(specificDate); // Output: Sun Jan 19 2025 10:20:30 GMT+0000 (Coordinated Universal Time)
1.3. Using Year, Month, Day, etc.
let customDate = new Date(2025, 0, 19, 10, 20, 30); console.log(customDate); // Output: Sun Jan 19 2025 10:20:30 GMT+0000 (Coordinated Universal Time)
Note: The month is zero-indexed (January is 0
).
2. Getting Date and Time Components
The Date
object provides several methods to get individual date and time components:
getFullYear()
: Returns the year.console.log(currentDate.getFullYear()); // Output: 2025
getMonth()
: Returns the month (0-11).console.log(currentDate.getMonth()); // Output: 0 (January)
getDate()
: Returns the day of the month (1-31).console.log(currentDate.getDate()); // Output: 19
getDay()
: Returns the day of the week (0-6).console.log(currentDate.getDay()); // Output: 0 (Sunday)
getHours()
: Returns the hour (0-23).console.log(currentDate.getHours()); // Output: 10
getMinutes()
: Returns the minutes (0-59).console.log(currentDate.getMinutes()); // Output: 20
getSeconds()
: Returns the seconds (0-59).console.log(currentDate.getSeconds()); // Output: 30
3. Setting Date and Time Components
You can set individual date and time components using the following methods:
setFullYear(year)
: Sets the year.currentDate.setFullYear(2030); console.log(currentDate.getFullYear()); // Output: 2030
setMonth(month)
: Sets the month (0-11).currentDate.setMonth(6); console.log(currentDate.getMonth()); // Output: 6 (July)
setDate(day)
: Sets the day of the month (1-31).currentDate.setDate(15); console.log(currentDate.getDate()); // Output: 15
setHours(hours)
: Sets the hour (0-23).currentDate.setHours(8); console.log(currentDate.getHours()); // Output: 8
4. Formatting Dates
JavaScript provides some basic methods to format dates:
toDateString()
: Converts the date to a readable string.console.log(currentDate.toDateString()); // Output: Sun Jan 15 2030
toISOString()
: Converts the date to an ISO 8601 string.console.log(currentDate.toISOString()); // Output: 2030-01-15T08:20:30.000Z
toLocaleDateString()
: Converts the date to a string based on the local date format.console.log(currentDate.toLocaleDateString()); // Output: 1/15/2030
5. Calculating Time Differences
You can calculate the difference between two dates by subtracting them, which returns the difference in milliseconds.
Example: Difference Between Two Dates
let date1 = new Date('2025-01-01'); let date2 = new Date('2025-01-19'); let difference = date2 - date1; let daysDifference = difference / (1000 * 60 * 60 * 24); console.log(daysDifference); // Output: 18
Summary of JavaScript Date
new Date()
: Used to create a date object with the current date and time or a specific date.getFullYear()
,getMonth()
,getDate()
: Methods to get individual date components.setFullYear()
,setMonth()
,setDate()
: Methods to set individual date components.- Formatting: Use
toDateString()
,toISOString()
, andtoLocaleDateString()
for date formatting. - Calculations: Subtract date objects to find the time difference in milliseconds.