J

JavaScript Handbook

Clean • Professional

JavaScript Date Reference

3 minute

JavaScript Date Reference

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.

What is a Date Object?

  • Represents a single point in time, stored as milliseconds since January 1, 1970, 00:00:00 UTC (Unix Epoch).
  • Can be manipulated using get, set, and formatting methods.
  • Can operate in local time or UTC depending on the method used.
const now = new Date();
console.log(now); // Thu Oct 09 2025 18:06:00 GMT+0530 (India Standard Time)

Creating Date Objects

No Arguments (Current Date & Time)

const now = new Date();
console.log(now);

Using a Date String

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

Using Individual Components

const date = new Date(2025, 9, 9, 18, 6, 0); // Month is 0-based
console.log(date); // 2025-10-09T18:06:00.000Z

Using Milliseconds (Unix Timestamp)

const date = new Date(1760148960000);
console.log(date); // Wed Oct 08 2025 18:19:22

JavaScript Date Input Formats

Format TypeSyntax / ExampleNotes
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
Milliseconds1760148960000Calculations & storage

JavaScript Date Output Formats

MethodExample OutputDescription
toString()Thu Oct 09 2025 18:06:00 GMT+0530Default local string
toDateString()Thu Oct 09 2025Human-readable date
toTimeString()18:06:00 GMT+0530Time only
toUTCString()Thu, 09 Oct 2025 12:36:00 GMTUTC time
toISOString()2025-10-09T12:36:00.000ZISO 8601 UTC
toLocaleString()10/9/2025, 6:06:00 PMLocalized date & time
toLocaleDateString()10/9/2025Localized date
toLocaleTimeString()6:06:00 PMLocalized time

Date Get Methods

Retrieve parts of a date object. Local time vs UTC variants exist.

MethodReturnsExample
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 epoch1760148960000
getTimezoneOffset()Minutes difference from UTC-330

Date Set Methods

Modify an existing Date object in place.

MethodParametersExample
setFullYear(year[, month, date])2026, 0, 15Sets year, optional month/day
setMonth(month[, date])0–11, 1–31date.setMonth(0) → January
setDate(date)1–31date.setDate(15)
setHours(hours[, min, sec, ms])0–23date.setHours(14,30)
setMinutes(min[, sec, ms])0–59date.setMinutes(45)
setSeconds(sec[, ms])0–59date.setSeconds(30)
setMilliseconds(ms)0–999date.setMilliseconds(500)
setTime(ms)Milliseconds since epochdate.setTime(1760148960000)

Date Reference & Cloning

  • Date objects are reference types. Assigning to another variable shares the same object.
  • To avoid accidental updates, clone using .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

Static Methods

MethodDescriptionExample
Date.now()Current timestamp1760148960000
Date.parse(string)Parse string to msDate.parse('2025-10-09') → 1760073600000
Date.UTC(year, month, …)Milliseconds for UTC dateDate.UTC(2025, 9, 9)

Practical Examples

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

 

Article 0 of 0