Skip to content

Working with Dates and Times

Programming Node.js Difficulty 1/5

Beginning of the lesson

Working with Dates and Times

Reading the clock, doing date math, and dodging the classic traps

Big Idea: Almost every real program needs to know when something happened: when a file was saved, when a user signed up, how many days are left until a birthday. JavaScript gives Node one built-in tool for all of this - the Date object. It is powerful, but it has a few famous surprises. Once you know them, dates stop being scary.
By the end you will be able to:
  • make a Date for now or for a specific day,
  • read the year, month, day, and time out of a date,
  • compare two dates and do arithmetic (like "how many days until?"),
  • understand timestamps, durations, and the zero-based-month, time-zone, and daylight-saving traps.

1. Making a Date

A Date is an object that holds one exact moment in time. You create one with new Date(...). There are three ways beginners use most:

const now = new Date();                 // right now, this very moment
const party = new Date(2026, 0, 15);    // a specific day: 15 January 2026
const iso = new Date('2026-07-15');     // from a text string (ISO format)

Watch out - months start at 0! In new Date(2026, 0, 15) the middle number is the month, and January is 0, not 1. So December is 11. The day-of-month, oddly, still starts at 1. This "zero-based month" is the single most famous Date trap - keep it in mind the whole way through.

Analogy. Think of the months as a shelf of 12 books numbered like array positions: the first book (January) is at slot 0, the last (December) is at slot 11. The day number, though, is written on the page like a normal calendar: the 15th is 15.

Calling new Date() with no arguments is the one you will use most: it captures the current date and time.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers