Working with Dates and Time in JavaScript (Node.js): From Basics to Advanced Techniques
Working with dates and time is one of the most common tasks in web development. Whether it's event logging, processing user data, task scheduling, or displaying timestamps — you can't do without knowing how to manage dates in JavaScript. In this article, we'll cover all key aspects of working with dates in the Node.js environment: from creation and formatting to complex timezone manipulations and using popular libraries.
Basics: The Date Object in JavaScript
The built-in Date object is the foundation for working with dates. It stores the number of milliseconds elapsed since January 1, 1970 (Unix epoch).
Creating a Date
There are several ways to create a Date instance:
// Current date and timeconst now = new Date();console.log(now); // 2025-03-15T10:30:00.000Z
// From milliseconds (Unix timestamp)const fromTimestamp = new Date(1742025000000);console.log(fromTimestamp); // 2025-03-15T10:30:00.000Z
// From a string (ISO 8601)const fromString = new Date('2025-03-15T10:30:00');console.log(fromString);
// From components (year, month, day, hour, minute, second, millisecond)const fromComponents = new Date(2025, 2, 15, 10, 30, 0, 0); // Months: 0-11console.log(fromComponents); // March 15, 2025 10:30:00Important: months in JavaScript are indexed from 0 (January is 0, December is 11).
Getting Date Components
Methods for extracting individual parts of a date:
const date = new Date('2025-03-15T10:30:45.123Z');
console.log(date.getFullYear()); // 2025console.log(date.getMonth()); // 2 (March)console.log(date.getDate()); // 15console.log(date.getDay()); // 6 (Saturday, 0 - Sunday)console.log(date.getHours()); // 10console.log(date.getMinutes()); // 30console.log(date.getSeconds()); // 45console.log(date.getMilliseconds());// 123console.log(date.getTime()); // milliseconds since epochFormatting Dates in Node.js
The standard Date provides limited formatting capabilities. For human-readable output, use toLocaleDateString() and toLocaleString().
const date = new Date('2025-03-15T10:30:00');
// Localized formattingconsole.log(date.toLocaleDateString('ru-RU')); // 15.03.2025console.log(date.toLocaleTimeString('ru-RU')); // 10:30:00console.log(date.toLocaleString('ru-RU')); // 15.03.2025, 10:30:00
// With optionsconst options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long', hour: '2-digit', minute: '2-digit'};console.log(date.toLocaleString('ru-RU', options)); // Saturday, March 15, 2025, 10:30Custom Formatting
For full control, write a simple function:
function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') { const map = { YYYY: date.getFullYear(), MM: String(date.getMonth() + 1).padStart(2, '0'), DD: String(date.getDate()).padStart(2, '0'), HH: String(date.getHours()).padStart(2, '0'), mm: String(date.getMinutes()).padStart(2, '0'), ss: String(date.getSeconds()).padStart(2, '0'), }; return format.replace(/YYYY|MM|DD|HH|mm|ss/g, match => map[match]);}
console.log(formatDate(new Date(), 'DD.MM.YYYY')); // 15.03.2025console.log(formatDate(new Date(), 'HH:mm:ss')); // 10:30:00Date Manipulations: Adding, Subtracting, Comparing
You can modify dates directly using set* methods or by working with milliseconds.
Adding and Subtracting Time
const date = new Date('2025-03-15T10:30:00');
// Add 5 daysdate.setDate(date.getDate() + 5);console.log(date); // 2025-03-20T10:30:00
// Add