JavaScript provides the Date object to work with dates and times. You can create date objects, read individual parts like year or month, and update dates using built-in methods.
Dates are commonly used for timestamps, scheduling, countdowns, logging, and showing user-friendly date and time information in web applications.
Date object is used for all date and time operations.getFullYear()) read parts of a date.setFullYear()) change parts of a date.toISOString() gives a standard machine-readable string.The Date constructor can be used in several ways:
new Date() — creates a date object with the current date and time.new Date(dateString) — parses a valid date string.new Date(year, month, day, hours, minutes, seconds, ms) — uses separate numeric parameters (month is 0–11).Common string formats include:
"2025-06-02""06/02/2025""June 2, 2025"Creating the current date and time, and specific dates:
const now = new Date();
console.log(now);
const d1 = new Date(); // Current date and time
const d2 = new Date("2025-06-02"); // Specific date string (ISO)
const d3 = new Date(2025, 5, 2); // Year, month (0-11), day
const d4 = new Date(2025, 5, 2, 14, 30); // Year, month, day, hour, minute
console.log(d1);
console.log(d2);
console.log(d3);
console.log(d4);
Using different string formats to create the same date:
const isoDate = new Date("2025-06-02"); // ISO format (recommended)
const shortDate = new Date("06/02/2025"); // Short format (MM/DD/YYYY or DD/MM/YYYY)
const longDate = new Date("June 2, 2025"); // Long format
console.log(isoDate);
console.log(shortDate);
console.log(longDate);
Use get methods to read parts of a date:
const date = new Date("2025-06-02T14:45:00");
console.log(date.getFullYear()); // 2025
console.log(date.getMonth()); // 5 (June, because months are 0-11)
console.log(date.getDate()); // 2 (day of the month)
console.log(date.getDay()); // 1 (day of the week: 0=Sunday, 1=Monday, ...)
console.log(date.getHours()); // 14
console.log(date.getMinutes()); // 45
console.log(date.getSeconds()); // 0
Use set methods to modify an existing date:
const d = new Date();
d.setFullYear(2030); // Set year to 2030
d.setMonth(11); // 11 = December
d.setDate(25); // 25th of the month
d.setHours(10); // 10 AM
d.setMinutes(30); // 30 minutes
console.log(d);
new Date(), you will see the current date and time on your system, for example: Mon Jun 02 2025 14:45:00 GMT+0530 (...).get methods, values like getMonth() and getDay() are numbers representing month index (0–11) and weekday (0–6).set methods updates the same Date object, so printing d again shows the modified date and time.toISOString(), you will get a standardized string like 2025-06-02T09:15:00.000Z, which is great for APIs and storage.0 = January, 11 = December).toISOString() for a standard, timezone-safe format."06/02/2025" because they may be interpreted differently (MM/DD vs DD/MM) in different environments.getDay() returns the weekday index (0–6), not the day of the month. Use getDate() for day of month.Date object for your next birthday.getDay() to find which weekday your next birthday falls on.setDate() to move that date 7 days ahead and log the new date.toISOString().DD-MM-YYYY as a string.