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 backend development. Whether it's event logging, deadline calculation, report generation, or API interaction, the ability to correctly handle timestamps is a critically important skill. In this article, we will cover all key aspects of working with dates in the Node.js environment: from the built-in Date object to popular libraries that simplify a developer's life.
1. The Built-in Date Object: Basics and Pitfalls
In JavaScript, the built-in Date object handles date and time operations. It provides basic methods for creating, reading, and manipulating dates. However, it has several features that are important to know.
Creating a Date Object
The simplest way to get the current date and time:
const now = new Date();console.log(now); // 2025-03-20T14:30:00.000Z (example)You can create a date from a string, a timestamp (milliseconds since January 1, 1970), or by specifying components manually:
// From a string (ISO 8601)const dateFromString = new Date("2025-12-25T10:00:00Z");
// From a timestampconst dateFromTimestamp = new Date(1735048800000);
// From components (year, month (0-11), day, hour, minute, second, millisecond)const dateFromParts = new Date(2025, 11, 25, 10, 0, 0, 0); // December 25, 2025 10:00:00console.log(dateFromParts); // 2025-12-25T07:00:00.000Z (depends on timezone)Important: Months in JavaScript are zero-indexed (January is 0, December is 11). This is a common source of errors.
Getting Date Components
The Date object provides methods for retrieving individual components:
const date = new Date();console.log(date.getFullYear()); // 2025console.log(date.getMonth()); // 2 (March, since counting starts from 0)console.log(date.getDate()); // 20console.log(date.getDay()); // 4 (Thursday, 0 - Sunday)console.log(date.getHours()); // 17console.log(date.getMinutes()); // 45console.log(date.getSeconds()); // 12console.log(date.getMilliseconds());// 345console.log(date.getTime()); // 1742474712345 (timestamp)Note: all these methods return values in the server's local timezone. To work with UTC, use methods with the getUTC... prefix (e.g., getUTCFullYear()).
Date Manipulation
Changing a date is done using set-methods. They mutate the original object:
const date = new Date();date.setFullYear(2026);date.setMonth(0); // Januarydate.setDate(15);console.log(date); // 2026-01-15T... (other components are preserved)Pitfall: set-methods automatically adjust the date. For example, if you set January 32, the date becomes February 1. This can be both convenient and unexpected.
2. Formatting Dates and Outputting in the Desired Format
The built-in toLocaleDateString() method allows you to output a date in the format accepted in a specific locale:
const date = new Date(2025, 11, 25);console.log(date.toLocaleDateString('ru-RU')); // 25.12.2025console.log(date.toLocaleDateString('en-US')); // 12/25/2025console.log(date.toLocaleDateString('de-DE')); // 25.12.2025For more flexible formatting, you can use Intl.DateTimeFormat:
const formatter = new Intl.DateTimeFormat('ru-RU', { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long', hour: '2-digit', minute: '2-digit'});console.log(formatter.format(new Date())); // "четверг, 20 марта 2025 г., 17:45"However, for complex formats (e.g., "2025-03-20T14:30:00+03:00") or custom templates, it is better to use libraries (more on them below).
Blogs
Book Recommendations
Read also
Mutation in Rust: Before or After the Colon? An Under-the-Hood Analysis
Working with WebAssembly and Rust: Advanced Integration Methods in Web
Dependency Injection in Swift: Principles and Examples for iOS/macOS
Working with Dates and Time in Kotlin: Advanced Techniques and API
Concurrency and Multithreading in Rust: From A to Z
Blogs
Book Recommendations
Read also
Dependency Injection in Swift: Principles and Examples for iOS/macOS