Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Accessing and Modifying Date Components | Working with Dates in JavaScript
Working with Dates and Times in JavaScript

bookAccessing and Modifying Date Components

JavaScript provides powerful setter methods for the Date object, allowing you to directly modify specific components of a date, such as the year, month, day, hour, minute, second, or millisecond. These methods are especially useful when you need to adjust a date after it has been created, whether you are updating a deadline, scheduling an event, or performing date arithmetic. The most commonly used setter methods include setFullYear(), setMonth(), setDate(), setHours(), setMinutes(), setSeconds(), and setMilliseconds(). Each method updates the corresponding part of the date, immediately changing the internal value of the Date object.

1234567891011
// Create a date for March 15, 2023 const meeting = new Date(2023, 2, 15); // Change the year to 2025 meeting.setFullYear(2025); // Change the month to December (remember: months are zero-based, so 11 = December) meeting.setMonth(11); console.log(meeting.toString()); // Output: Sun Dec 15 2025 00:00:00 GMT+0000 (Coordinated Universal Time)
copy
12345678
// Create a date for August 10, 2024 const projectDeadline = new Date(2024, 7, 10); // Add 5 days to the deadline projectDeadline.setDate(projectDeadline.getDate() + 5); console.log(projectDeadline.toString()); // Output: Thu Aug 15 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
copy

When you use setter methods like setDate(), JavaScript automatically handles values that go beyond the normal range for that component. For example, if you add days to a date and the total exceeds the number of days in the current month, the Date object will "overflow" into the next month as needed. In the previous code sample, adding 5 days to August 10 results in August 15, which is still within the same month, but if you were to add 25 days, the date would move into September. This behavior applies to other setters as well—setting the month to a value higher than 11, for instance, will increase the year accordingly. This automatic adjustment makes date arithmetic in JavaScript both flexible and convenient, reducing the need for manual calculation of month lengths or leap years.

question mark

Which of the following methods can be used to modify specific components of a JavaScript Date object?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain how the month indexing works in JavaScript dates?

What happens if I set a date component to a value outside its normal range?

Can you show more examples of using other setter methods like setHours or setMinutes?

Awesome!

Completion rate improved to 7.14

bookAccessing and Modifying Date Components

Desliza para mostrar el menú

JavaScript provides powerful setter methods for the Date object, allowing you to directly modify specific components of a date, such as the year, month, day, hour, minute, second, or millisecond. These methods are especially useful when you need to adjust a date after it has been created, whether you are updating a deadline, scheduling an event, or performing date arithmetic. The most commonly used setter methods include setFullYear(), setMonth(), setDate(), setHours(), setMinutes(), setSeconds(), and setMilliseconds(). Each method updates the corresponding part of the date, immediately changing the internal value of the Date object.

1234567891011
// Create a date for March 15, 2023 const meeting = new Date(2023, 2, 15); // Change the year to 2025 meeting.setFullYear(2025); // Change the month to December (remember: months are zero-based, so 11 = December) meeting.setMonth(11); console.log(meeting.toString()); // Output: Sun Dec 15 2025 00:00:00 GMT+0000 (Coordinated Universal Time)
copy
12345678
// Create a date for August 10, 2024 const projectDeadline = new Date(2024, 7, 10); // Add 5 days to the deadline projectDeadline.setDate(projectDeadline.getDate() + 5); console.log(projectDeadline.toString()); // Output: Thu Aug 15 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
copy

When you use setter methods like setDate(), JavaScript automatically handles values that go beyond the normal range for that component. For example, if you add days to a date and the total exceeds the number of days in the current month, the Date object will "overflow" into the next month as needed. In the previous code sample, adding 5 days to August 10 results in August 15, which is still within the same month, but if you were to add 25 days, the date would move into September. This behavior applies to other setters as well—setting the month to a value higher than 11, for instance, will increase the year accordingly. This automatic adjustment makes date arithmetic in JavaScript both flexible and convenient, reducing the need for manual calculation of month lengths or leap years.

question mark

Which of the following methods can be used to modify specific components of a JavaScript Date object?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
some-alt