Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Setting and Getting Date Components | Date Manipulation and Comparison
JavaScript Date Handling with Day.js

bookSetting and Getting Date Components

index.html

index.html

copy

When working with Day.js to access or change parts of a date, you use getter and setter methods like year(), month(), date(), hour(), minute(), and second(). Each getter returns the current value for that part of the date. To set a component, pass a value to the method, which returns a new Day.js object with the updated value.

A key detail to remember is that months in Day.js are zero-based: January is 0, February is 1, and December is 11. This matches the JavaScript Date object, so always subtract one from the human-readable month number.

Day.js is immutable. This means that any setter method like year(2025) or month(0) does not change the original date object—it creates and returns a new Day.js instance with the modification. This prevents accidental changes and makes your code more predictable.

For best results when manipulating date components:

  • Always assign the result of a setter to a new variable;
  • Be careful with zero-based months to avoid off-by-one errors;
  • Chain setters if you need to update multiple components in sequence;
  • Use format() to display the result in a human-friendly way.
question mark

In Day.js, what is important to remember when setting the month component of a date?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookSetting and Getting Date Components

Свайпніть щоб показати меню

index.html

index.html

copy

When working with Day.js to access or change parts of a date, you use getter and setter methods like year(), month(), date(), hour(), minute(), and second(). Each getter returns the current value for that part of the date. To set a component, pass a value to the method, which returns a new Day.js object with the updated value.

A key detail to remember is that months in Day.js are zero-based: January is 0, February is 1, and December is 11. This matches the JavaScript Date object, so always subtract one from the human-readable month number.

Day.js is immutable. This means that any setter method like year(2025) or month(0) does not change the original date object—it creates and returns a new Day.js instance with the modification. This prevents accidental changes and makes your code more predictable.

For best results when manipulating date components:

  • Always assign the result of a setter to a new variable;
  • Be careful with zero-based months to avoid off-by-one errors;
  • Chain setters if you need to update multiple components in sequence;
  • Use format() to display the result in a human-friendly way.
question mark

In Day.js, what is important to remember when setting the month component of a date?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2
some-alt