Skip to content
Home ยป The Date Object

The Date Object


๐Ÿง  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)

MethodDescription
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)

MethodDescription
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

MethodOutput
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

FeatureUse
new Date()Current date
get methodsRead values
set methodsModify values
toString methodsFormat output
calculationsDate 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.