Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Adding and Subtracting Time | Date Manipulation and Comparison
JavaScript Date Handling with Day.js

bookAdding and Subtracting Time

index.html

index.html

copy

When working with dates, you often need to adjust them by adding or subtracting time units. Day.js provides the add and subtract methods for this purpose. Both methods accept two arguments: the amount to change and the unit of time, such as "days", "months", or "years". For example, you can call add(5, "days") to move a date forward by five days, or subtract(2, "months") to go back two months. These methods return a new Day.js object, so the original date remains unchanged.

You can also chain these operations together for more complex manipulations. For instance, you might want to add one year and then subtract ten days in a single expression. Chaining allows you to perform multiple adjustments in sequence, making your code concise and readable. This approach is helpful when you need to compute dates for reminders, deadlines, or scheduling tasks.

question mark

Which Day.js method would you use to add 5 days to a date?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookAdding and Subtracting Time

Deslize para mostrar o menu

index.html

index.html

copy

When working with dates, you often need to adjust them by adding or subtracting time units. Day.js provides the add and subtract methods for this purpose. Both methods accept two arguments: the amount to change and the unit of time, such as "days", "months", or "years". For example, you can call add(5, "days") to move a date forward by five days, or subtract(2, "months") to go back two months. These methods return a new Day.js object, so the original date remains unchanged.

You can also chain these operations together for more complex manipulations. For instance, you might want to add one year and then subtract ten days in a single expression. Chaining allows you to perform multiple adjustments in sequence, making your code concise and readable. This approach is helpful when you need to compute dates for reminders, deadlines, or scheduling tasks.

question mark

Which Day.js method would you use to add 5 days to a date?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
some-alt