The JavaScript Date Object: A Guide to Time

Master JavaScript's built-in object for handling dates and times, from creating timestamps to building countdown timers.

📅

Welcome! Let's explore how JavaScript handles time with the powerful Date object.

/* Ready to manage time? */

Creating a Date

In JavaScript, the Date object is your tool for all things time-related. You create an "instance" of a date using the new Date() constructor. Without any arguments, it captures the exact date and time the code is run.

Getting Date Components

Once you have a date object, you can extract specific parts using "getter" methods. For example, .getFullYear() returns the 4-digit year, and .getDate() returns the day of the month (1-31). Watch out: .getMonth() is zero-indexed, meaning January is 0 and December is 11.

Setting Date Components

Just as you can get parts of a date, you can also change them with "setter" methods like .setFullYear(2030) or .setMonth(0). These methods modify the date object directly, allowing you to manipulate time in your code.

Timestamps: The Core of Time

Under the hood, every date is stored as a single number: the milliseconds that have passed since midnight on January 1, 1970 (the "Unix Epoch"). You can get this value with .getTime(). This is incredibly useful for comparing dates or calculating durations.

Practice Zone


Interactive Test 1: Match the Methods

Drag the date methods to their correct descriptions.

Arrastra en el orden correspondiente.


Arrastra las opciones:

getFullYear()
getMonth()
getDate()
setHours()

Completa el código:

Method to get the 4-digit year.______
Method to get the month (0-11).______
Method to get the day of the month (1-31).______
Method to change the hour of a date.______
Unlock with Premium

Interactive Test 2: Complete the Code

Complete the code to get the current year from the `fecha` date object.

Rellena los huecos en cada casilla.

const fecha = new Date();
console.log(fecha.());
Unlock with Premium

Practice Example: Code Editor

In the editor, create a new constant called `fecha` that holds the current date, and then print it to the console.

Enunciado:

* Escribe el código a continuación. Los caracteres correctos se mostrarán en verde y los incorrectos en rojo.

const fecha = new Date(); console.log(fecha);
Unlock with Premium

Knowledge Check

Which method is used to get the month of a date in JavaScript?


Unlock with Premium

The Date Object in Action

The `Date` object is more than just telling time; it's the engine behind timers, post timestamps, and scheduling features. Here are some real-world examples.


1. Displaying Formatted Dates

You rarely show users the raw `Date` object output. Methods like `toLocaleDateString()` format it into a user-friendly string based on their region, perfect for blog posts or comments.

const postDate = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric' };

document.querySelector('#date')
  .textContent = postDate.toLocaleDateString('en-US', options);

Posted on: October 2, 2025

2. Building a Countdown Timer

By getting the timestamp (`.getTime()`) of a future date and the current date, you can calculate the difference in milliseconds. Then, it's just a matter of math to convert that into days, hours, minutes, and seconds.

const futureDate = new Date('Jan 1, 2026');
const now = new Date();

const diff = futureDate.getTime() - now.getTime();

const daysLeft = Math.floor(diff / (1000 * 60 * 60 * 24));
90 Days Left

Practical Takeaway: Master the `Date` object to handle anything time-related, from simple UI text to complex application logic like scheduling and countdowns.

Date Object Glossary

new Date()
The constructor used to create a new Date object. It can be called with a date string, numbers, or empty to get the current time.
Timestamp
The number of milliseconds that have passed since the Unix Epoch (00:00:00 UTC on 1 January 1970). Retrieved with `.getTime()`.
Getter Methods (e.g., .getFullYear())
Functions that "get" or return a specific piece of information from the date object, such as the year, month, or hour.
Setter Methods (e.g., .setFullYear())
Functions that "set" or modify a part of the date object.
.getMonth()
Returns the month as a zero-based index, where 0 is January and 11 is December. This is a very common source of bugs for new developers!
.toLocaleDateString()
Returns a string with a language-sensitive representation of the date portion. Ideal for displaying dates to users.