In JavaScript, the Date object is a specialized built in object designed to handle time. It is a Reference Type.
At its heart, a Date is a wrapper around a single numeric value.
[[DateValue]]): Every Date object contains a hidden internal slot that stores a 64-bit integer called Millisecond offset.Date instance lives in the Heap Memory. The Slot Memory assigned to variable identifier in variable environment or lexical environment record holds the Heap address.Before using the API, we must understand the Language of time used by the engine.
UTC is the primary time standard by which the world regulates clocks and time. It is not a timezone itself, but the high-precision reference point (0° longitude) from which all timezones are calculated as offsets (e.g., UTC+5:30).
This is the internationally accepted way to represent dates and times as strings. JavaScript uses a simplified version of this.
YYYY-MM-DDTHH:mm:ss.sssZThe Date() constructor is overloaded. Depending on what we pass, the engine performs different memory allocation tasks.
// 1. Current Time (No Arguments)
// Allocates a new object with the current system millisecond
const now = new Date();
// 2. Unix Timestamp (Single Number)
// Directly sets the internal [[DateValue]]
const fromMs = new Date(1734566400000);
// 3. Date String (ISO 8601)
// The engine parses the string. Safest way to avoid cross-browser bugs.
const fromString = new Date("2025-12-18T10:00:00Z");
// 4. Component Parts (Multiple Numbers)
// Note: Month is 0-indexed.
// Signature: (year, monthIndex, day, hours, minutes, seconds, ms)
const components = new Date(2025, 11, 25, 10, 30); // Dec 25, 2025, 10:30 AMThe Engine provides three types of methods to interact with the [[DateValue]] in the Heap.
Called on the Date class. These do not require new and return Numbers, not objects.
const ts = Date.now(); // Current timestamp (Fastest)
const ms = Date.parse("2025-01-01"); // String -> NumberThese read the internal slot and return Primitives.
const d = new Date();
console.log(d.getTime()); // The raw 64-bit integer
console.log(d.getFullYear()); // Year in Local Time
console.log(d.getUTCFullYear()); // Year in UTC TimeWarning: These modify the object in the Heap directly.
const d = new Date();
d.setFullYear(2030); // The object at this memory address is now changedDate objects are mutable. Methods like .setFullYear(), .setHours(), or .setDate() modify the value stored in the Heap directly. They do not return a new object.
const eventDate = new Date("2025-12-31");
const reminderDate = eventDate; // Both point to the same Heap address
// Mutating the reminder silently changes the event
reminderDate.setMonth(0); // Changes month to January
console.log(eventDate.toLocaleDateString()); // "1/31/2025" -> Unexpected!Best Practice: To avoid this, always clone a date before modifying it using
new Date(originalDate).
Because Dates are objects, standard comparison operators (== or ===) behave differently than they do with primitives.
=== checks if they point to the same memory address, not if they represent the same time.| Operation | Comparison Type | Result |
|---|---|---|
dateA === dateB | Reference | true only if they are the exact same object instance. |
+dateA === +dateB | Value | true if they represent the same millisecond (Unary + coerces to Number). |
dateA.getTime() === dateB.getTime() | Value | The explicit way to compare the 64-bit internal values. |
Dates have a unique and often problematic relationship with data transmission.
JSON.stringify() encounters a Date, it automatically converts it into an ISO 8601 String (e.g., "2025-12-18T14:00:00.000Z").JSON.parse(), the engine does not recognize that string as a Date. It remains a plain String in memory..getDay() or .getFullYear().const user = { joined: new Date() };
const serialized = JSON.stringify(user);
const parsed = JSON.parse(serialized);
// parsed.joined is now a STRING, not a Date object.
// parsed.joined.getFullYear() will throw a TypeError.| Feature | Behavior |
|---|---|
| Type | Object (Reference Type) |
| Storage | Heap Memory (64-bit integer internal slot) |
| Mutability | Mutable (Methods modify the object in-place) |
| Comparison | By Reference (use .getTime() for value comparison) |
| JSON Behavior | Converts to ISO String (requires manual re-instantiation) |