← Back to Chapters

JavaScript Dates

? JavaScript Dates

⚡ Quick Overview

JavaScript provides the Date object to work with dates and times. You can create date objects, read individual parts like year or month, and update dates using built-in methods.

Dates are commonly used for timestamps, scheduling, countdowns, logging, and showing user-friendly date and time information in web applications.

? Key Concepts

  • Date object is used for all date and time operations.
  • You can create dates using no arguments, strings, or separate numeric parts.
  • Different date formats exist: ISO, short, and long formats.
  • Get methods (like getFullYear()) read parts of a date.
  • Set methods (like setFullYear()) change parts of a date.
  • Months in JavaScript are 0-indexed (0 = January, 11 = December).
  • toISOString() gives a standard machine-readable string.

? Syntax and Theory

The Date constructor can be used in several ways:

  • new Date() — creates a date object with the current date and time.
  • new Date(dateString) — parses a valid date string.
  • new Date(year, month, day, hours, minutes, seconds, ms) — uses separate numeric parameters (month is 0–11).

Common string formats include:

  • ISO: "2025-06-02"
  • Short: "06/02/2025"
  • Long: "June 2, 2025"

? Code Examples

? Creating Date Objects

Creating the current date and time, and specific dates:

? View Code Example
const now = new Date();
console.log(now);

const d1 = new Date();                             // Current date and time
const d2 = new Date("2025-06-02");                // Specific date string (ISO)
const d3 = new Date(2025, 5, 2);                  // Year, month (0-11), day
const d4 = new Date(2025, 5, 2, 14, 30);          // Year, month, day, hour, minute

console.log(d1);
console.log(d2);
console.log(d3);
console.log(d4);

? Working with Date Formats

Using different string formats to create the same date:

? View Code Example
const isoDate = new Date("2025-06-02");       // ISO format (recommended)
const shortDate = new Date("06/02/2025");    // Short format (MM/DD/YYYY or DD/MM/YYYY)
const longDate = new Date("June 2, 2025");   // Long format

console.log(isoDate);
console.log(shortDate);
console.log(longDate);

? Reading Date Parts (Get Methods)

Use get methods to read parts of a date:

? View Code Example
const date = new Date("2025-06-02T14:45:00");

console.log(date.getFullYear());  // 2025
console.log(date.getMonth());     // 5 (June, because months are 0-11)
console.log(date.getDate());      // 2 (day of the month)
console.log(date.getDay());       // 1 (day of the week: 0=Sunday, 1=Monday, ...)
console.log(date.getHours());     // 14
console.log(date.getMinutes());   // 45
console.log(date.getSeconds());   // 0

?️ Updating Date Parts (Set Methods)

Use set methods to modify an existing date:

? View Code Example
const d = new Date();

d.setFullYear(2030); // Set year to 2030
d.setMonth(11);    // 11 = December
d.setDate(25);     // 25th of the month
d.setHours(10);    // 10 AM
d.setMinutes(30);  // 30 minutes

console.log(d);

? Live Output and Explanation

? What You Will See in the Console

  • For new Date(), you will see the current date and time on your system, for example: Mon Jun 02 2025 14:45:00 GMT+0530 (...).
  • For get methods, values like getMonth() and getDay() are numbers representing month index (0–11) and weekday (0–6).
  • Using set methods updates the same Date object, so printing d again shows the modified date and time.
  • If you use toISOString(), you will get a standardized string like 2025-06-02T09:15:00.000Z, which is great for APIs and storage.

✅ Helpful Tips

  • Remember: months are 0-indexed (0 = January, 11 = December).
  • Prefer toISOString() for a standard, timezone-safe format.
  • JavaScript automatically adjusts invalid dates (for example, Feb 30 becomes Mar 1).
  • Be careful with regional short formats like "06/02/2025" because they may be interpreted differently (MM/DD vs DD/MM) in different environments.
  • getDay() returns the weekday index (0–6), not the day of the month. Use getDate() for day of month.

? Practice Tasks

  • Create a Date object for your next birthday.
  • Use getDay() to find which weekday your next birthday falls on.
  • Use setDate() to move that date 7 days ahead and log the new date.
  • Print your birthday date using toISOString().
  • Build a small function that returns today's date in the format DD-MM-YYYY as a string.