JavaScript Fundamentals / Date and Times

Date Object

📌 Default Behavior

  • JavaScript uses the browser's local timezone

  • Display format example:

console.log(new Date().toString());
// Tue Jul 01 2025 12:30:25 GMT+0530 (India Standard Time)

⚠️ Output will vary based on system time & timezone.


⚡️ Creating Date Objects

Date objects are created using:

new Date()
new Date(dateString)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, ms)
new Date(milliseconds)

1) new Date()

Creates current date & time.

const d = new Date();
console.log(d.toString()); // Tue Jul 01 2025 12:30:25 GMT+0530 (India Standard Time)

2) new Date(dateString)

Parses a valid date string.

const d = new Date("Feb 13, 2014 11:13:00");
console.log(d.toString()); // Thu Feb 13 2014 11:13:00 GMT+0530 (India Standard Time)

⚠️ Note: parsing non-ISO formats can differ between browsers.


3) new Date(year, month, day, hour, minute, second, ms)

const d = new Date(2018, 11, 24, 10, 33, 30, 0);
console.log(d.toString()); // Mon Dec 24 2018 10:33:30 GMT+0530 (India Standard Time)

✅ Month overflow example

Specifying a month higher than 11, will not result in an error but add the overflow to the next year.

const d = new Date(2018, 15, 24, 10, 33, 30, 0);
console.log(d.toString()); // Sun Apr 24 2019 10:33:30 GMT+0530 (India Standard Time)

✅ Day overflow example

Specifying a day higher than max, will not result in an error but add the overflow to the next month.

const d = new Date(2018, 4, 35, 10, 33, 30, 0);
console.log(d.toString()); // Mon Jun 04 2018 10:33:30 GMT+0530 (India Standard Time)

📌 JavaScript Stores Dates as Milliseconds

JavaScript stores dates as number of milliseconds since January 01, 1970.

🪄new Date(milliseconds)

new Date(*milliseconds*) creates a new date object as milliseconds plus zero time.

// 1 day = 24 * 60 * 60 * 1000 = 86400000 milliseconds
let nextDay = new Date(86400000);  // Jan 2, 1970
let twoDaysLater = new Date(86400000 * 2);  // Jan 3, 1970
let pastDate = new Date(-86400000); // Dec 31, 1969
  • The number is relative to Jan 1, 1970.

  • Positive numbers = future dates.

  • Negative numbers = past dates.

const d = new Date(100000000000);
console.log(d.toString()); // Sat Mar 03 1973 15:16:40 GMT+0530 (India Standard Time)

const d2 = new Date(-100000000000);
console.log(d2.toString()); // Mon Oct 31 1966 22:43:20 GMT+0530 (India Standard Time)