📅 JavaScript Date Object
Last Updated: 26th October 2025
- JavaScript provides the Date object to work with dates and times.
- You can create, manipulate, and format dates using this object.
Hinglish Tip 🗣: Date object ko socho ek “calendar & clock machine” ke jaise — abhi ka time de sakta hai, future date bana sakte ho, aur differences bhi nikal sakte ho.
✏ Creating Date Objects
// Current date & time
let now = new Date();
console.log(now);
// Specific date
let birthday = new Date("2000-01-01");
console.log(birthday);
// Using year, month, day, hour, min, sec
let specific = new Date(2025, 9, 26, 22, 30, 0); // month is 0-indexed
console.log(specific);
Common Date Methods
- getFullYear(): Returns the year as a four-digit number.
- getMonth(): Returns the month as a zero-based number (0-11).
- getDate(): Returns the day of the month as a number (1-31).
- getDay(): Returns the day of the week as a number (0-6).
- getHours(): Returns the hour as a number (0-23).
- getMinutes(): Returns the minutes as a number (0-59).
- getSeconds(): Returns the seconds as a number (0-59).
- getMilliseconds(): Returns the milliseconds as a number (0-999).
- getTime(): Returns the number of milliseconds since January 1, 1970, UTC.
Setting Date & Time
- setFullYear(): Sets the year.
- setMonth(): Sets the month (0-11).
- setDate(): Sets the day of the month.
- setHours(): Sets the hour (0-23).
- setMinutes(): Sets the minutes (0-59).
- setSeconds(): Sets the seconds (0-59).
- setMilliseconds(): Sets the milliseconds (0-999).
let now = new Date();
now.setFullYear(2025);
console.log(now);
Formatting Dates
- toDateString(): Returns a string representation of the date in the local time zone.
- toTimeString(): Returns a string representation of the time in the local time zone.
- toLocaleString(): Returns a localized string representation of the date.
- toLocaleDateString(): Returns a localized string representation of the date without the time.
- toLocaleTimeString(): Returns a localized string representation of the time without the date.
let now = new Date();
console.log(now.toDateString()); // Sat Oct 26 2025
console.log(now.toTimeString()); // 22:30:00 GMT+0530 (India Standard Time)
console.log(now.toLocaleDateString()); // 26/10/2025
console.log(now.toLocaleTimeString()); // 22:30:00