⚡ JavaScript

العمل مع التاريخ والوقت في JavaScript (Node.js): دليل مفصل

What is it and why? (Real-life analogy)

Imagine you are the organizer of a school camp. You have a schedule: breakfast at 8:30, lunch at 13:00, lights out at 21:00. You need to know how much time is left until lunch, how many days until the camp session ends, or what day of the week Parent's Day will be. All of this involves working with dates and times.

In programming, date and time are not just numbers on a calendar. They are tools for:

  • Logging — recording when an event occurred (e.g., a user registered on March 15, 2025 at 14:32)
  • Scheduling — running tasks on a schedule (send an email in 2 days)
  • Calculations — how many days passed between two events
  • Formatting — displaying a date in a readable format: "Today, April 10" or "10.04.2025"
Important: In JavaScript (and Node.js), working with dates and times is one of the most common tasks. No serious application can do without it: from a simple alarm clock to a complex ticket booking system.

Theory: How it works internally

At the core of everything is the timestamp — the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. This date is called the "Unix Epoch".

Why 1970? Because the Unix operating system was created in the late 1960s, and the developers chose this date as the starting point. Since then, all computers in the world have "agreed" to count time this way.

In JavaScript, date and time are represented by the Date object. Inside it, one number is stored — the number of milliseconds since the Unix Epoch. Everything else (year, month, day, hour, minute, second) is calculated from this number.

It is important to understand the difference between:

  • UTC (Coordinated Universal Time) — the "base" time from which all time zones are calculated.
  • Local time — the time in your time zone. For example, Moscow is UTC+3.
Remember: Inside the computer, all dates are stored in UTC. When you see a date on the screen, it is already a value converted to your time zone.

Syntax in JavaScript (Node.js): All forms and options

Creating a Date object

In JavaScript, there are 4 main ways to create a date object:

Method Example Result
Current date and time new Date() Current moment in time
From milliseconds new Date(1700000000000) Date corresponding to those milliseconds
From a string new Date("2025-03-15T14:30:00") Date parsed from the string
From components new Date(2025, 2, 15, 14, 30, 0) Date from year, month, day, etc.
Attention: Months in JavaScript are numbered starting from 0! January is 0, February is 1, December is 11. This is one of the most common mistakes beginners make.

Basic methods of the Date object

All methods are divided into two groups: "get" and "set".

Method What it returns Value range
getFullYear() Year (4 digits) 1970 — 9999
getMonth() Month (0-11) 0 (January) — 11 (December)
getDate() Day of the month (1-31) 1 — 31
getDay() Day of the week (0-6) 0 (Sunday) — 6 (Saturday)
getHours() Hours (0-23) 0 — 23
getMinutes() Minutes (0-59) 0 — 59
getSeconds() Seconds (0-59) 0 — 59
getMilliseconds() Milliseconds (0-999) 0 — 999
getTime() Milliseconds since Unix Epoch A very large number

For UTC versions of the methods, add "UTC" to the name: getUTCFullYear(), getUTCMonth(), etc.

The set methods work similarly: setFullYear(), setMonth(), setDate(), etc.

Date formatting

Built-in methods for converting a date to a string:

Method Example output
toString() "Sat Mar 15 2025 14:30:00 GMT+0300"
toDateString() "Sat Mar 15 2025"
toTimeString() "14:30:00 GMT+0300"
toISOString() "2025-03-15T11:30:00.000Z"
toLocaleDateString() "15.03.2025" (depends on locale)
toLocaleTimeString() "14:30:00" (depends on locale)

Code examples — from simple to complex

Example 1: Getting the current date and time