Clean β’ Professional
JavaScript Date objects are essential for handling dates, times, and timestamps in web development. Understanding their creation, manipulation, formatting, and reference behavior is crucial for reliable applications.
const now = new Date();
console.log(now); // Thu Oct 09 2025 18:06:00 GMT+0530 (India Standard Time)
const now = new Date();
console.log(now);
ISO 8601 format is recommended for consistency:
const date = new Date('2025-10-09'); // Local time
console.log(date); // 2025-10-09T00:00:00.000Z
const date = new Date(2025, 9, 9, 18, 6, 0); // Month is 0-based
console.log(date); // 2025-10-09T18:06:00.000Z
const date = new Date(1760148960000);
console.log(date); // Wed Oct 08 2025 18:19:22
| Format Type | Syntax / Example | Notes |
|---|---|---|
| ISO | "2025-10-08" | Standard, cross-browser reliable |
| ISO with Time | "2025-10-08T18:30:00" | Local time |
| ISO UTC | "2025-10-08T18:30:00Z" | UTC, ideal for APIs |
| Short (US) | "10/08/2025" | Browser-dependent |
| Long | "October 8, 2025" | Human-readable |
| Milliseconds | 1760148960000 | Calculations & storage |
| Method | Example Output | Description |
|---|---|---|
toString() | Thu Oct 09 2025 18:06:00 GMT+0530 | Default local string |
toDateString() | Thu Oct 09 2025 | Human-readable date |
toTimeString() | 18:06:00 GMT+0530 | Time only |
toUTCString() | Thu, 09 Oct 2025 12:36:00 GMT | UTC time |
toISOString() | 2025-10-09T12:36:00.000Z | ISO 8601 UTC |
toLocaleString() | 10/9/2025, 6:06:00 PM | Localized date & time |
toLocaleDateString() | 10/9/2025 | Localized date |
toLocaleTimeString() | 6:06:00 PM | Localized time |
Retrieve parts of a date object. Local time vs UTC variants exist.
| Method | Returns | Example |
|---|---|---|
getFullYear() | Year (4 digits) | 2025 |
getMonth() | Month (0β11) | 9 (October) |
getDate() | Day of month (1β31) | 9 |
getDay() | Day of week (0β6) | 4 (Thursday) |
getHours() | Hours (0β23) | 18 |
getMinutes() | Minutes (0β59) | 6 |
getSeconds() | Seconds (0β59) | 0 |
getMilliseconds() | Milliseconds (0β999) | 0 |
getTime() | Milliseconds since epoch | 1760148960000 |
getTimezoneOffset() | Minutes difference from UTC | -330 |
Modify an existing Date object in place.
| Method | Parameters | Example |
|---|---|---|
setFullYear(year[, month, date]) | 2026, 0, 15 | Sets year, optional month/day |
setMonth(month[, date]) | 0β11, 1β31 | date.setMonth(0) β January |
setDate(date) | 1β31 | date.setDate(15) |
setHours(hours[, min, sec, ms]) | 0β23 | date.setHours(14,30) |
setMinutes(min[, sec, ms]) | 0β59 | date.setMinutes(45) |
setSeconds(sec[, ms]) | 0β59 | date.setSeconds(30) |
setMilliseconds(ms) | 0β999 | date.setMilliseconds(500) |
setTime(ms) | Milliseconds since epoch | date.setTime(1760148960000) |
.getTime():const date1 = new Date('2025-10-09T18:06:00+05:30');
const date2 = new Date(date1.getTime()); // Independent copy
date2.setDate(20);
console.log(date1.toLocaleString()); // 10/9/2025, 6:06 PM
console.log(date2.toLocaleString()); // 10/20/2025, 6:06 PM
| Method | Description | Example |
|---|---|---|
Date.now() | Current timestamp | 1760148960000 |
Date.parse(string) | Parse string to ms | Date.parse('2025-10-09') β 1760073600000 |
Date.UTC(year, month, β¦) | Milliseconds for UTC date | Date.UTC(2025, 9, 9) |
Create, Get, Set, Format Dates
const date = new Date('2025-10-09T18:06:00+05:30');
// Get components
console.log(date.getFullYear()); // 2025
console.log(date.getMonth()); // 9
console.log(date.getDate()); // 9
// Set components
date.setFullYear(2026);
date.setMonth(0); // January
date.setDate(15);
date.setHours(14, 30);
console.log(date.toLocaleString('en-US')); // 1/15/2026, 2:30:00 PM
// UTC example
console.log(date.getUTCHours()); // 9
date.setUTCHours(12);
console.log(date.toISOString()); // 2026-01-15T12:00:00.000Z
Calculate Time Difference
const start = new Date('2025-10-09T18:06:00+05:30');
const end = new Date('2025-10-10T18:06:00+05:30');
const diffDays = (end - start) / (1000 * 60 * 60 * 24);
console.log(diffDays); // 1Β