๐ง 1. Introduction to Date Object
The Date Object in JavaScript is used to work with dates and time.
๐ It allows you to:
- Get current date and time
- Set custom dates
- Perform date calculations
โ Creating a Date Object:
let today = new Date();
console.log(today);
๐ 2. Ways to Create Date Object
(1) Current Date & Time
let d = new Date();
(2) Specific Date (String Format)
let d = new Date("2025-01-01");
(3) Using Parameters
let d = new Date(year, month, day, hours, minutes, seconds);
๐ Example:
let d = new Date(2025, 0, 1); // January (month starts from 0)
(4) Using Milliseconds
let d = new Date(0); // Jan 1, 1970
โ ๏ธ Important Note
๐ Months are 0-based (0 = January, 11 = December)
๐ 3. Getting Date Values (Getter Methods)
| Method | Description |
|---|---|
getFullYear() | Year |
getMonth() | Month (0โ11) |
getDate() | Day of month |
getDay() | Day of week (0โ6) |
getHours() | Hours |
getMinutes() | Minutes |
getSeconds() | Seconds |
getMilliseconds() | Milliseconds |
getTime() | Milliseconds since 1970 |
๐ป Example:
let d = new Date();
console.log(d.getFullYear());
console.log(d.getMonth());
console.log(d.getDate());
console.log(d.getHours());
โ๏ธ 4. Setting Date Values (Setter Methods)
| Method | Description |
|---|---|
setFullYear() | Set year |
setMonth() | Set month |
setDate() | Set day |
setHours() | Set hours |
๐ป Example:
let d = new Date();
d.setFullYear(2030);
d.setMonth(5);
d.setDate(15);
console.log(d);
โฑ๏ธ 5. Date Formats
(a) ISO Format
let d = new Date("2025-05-01");
(b) Short Format
let d = new Date("05/01/2025");
(c) Full Format
let d = new Date("May 1, 2025");
๐ 6. Converting Date to String
| Method | Output |
|---|---|
toDateString() | Date only |
toTimeString() | Time only |
toISOString() | Standard format |
toLocaleDateString() | Local date |
๐ป Example:
let d = new Date();
console.log(d.toDateString());
console.log(d.toTimeString());
๐ข 7. Date Calculations
Example: Difference between two dates
let d1 = new Date("2025-01-01");
let d2 = new Date("2025-01-10");
let diff = d2 - d1;
console.log(diff / (1000 * 60 * 60 * 24)); // days
โฐ 8. Working with Time
Current Time:
let d = new Date();
console.log(d.getHours() + ":" + d.getMinutes());
๐ 9. Complete Example
let now = new Date();
console.log("Year:", now.getFullYear());
console.log("Month:", now.getMonth());
console.log("Date:", now.getDate());
console.log("Day:", now.getDay());
console.log("Time:", now.getHours() + ":" + now.getMinutes());
now.setFullYear(2030);
console.log("Updated Year:", now.getFullYear());
๐ Summary Table
| Feature | Use |
|---|---|
| new Date() | Current date |
| get methods | Read values |
| set methods | Modify values |
| toString methods | Format output |
| calculations | Date difference |
โ ๏ธ Important Points
- Month starts from 0
- Date object stores time in milliseconds
- Useful for:
- Timers
- Scheduling
- Event tracking
๐ฏ Conclusion
The Date Object is essential for:
- Handling date and time operations
- Performing calculations
- Creating dynamic applications
๐ It is widely used in real-time systems, booking apps, and scheduling systems.
