JavaScript Set Date Methods

JavaScript provides a number of methods for setting the date and time of a Date object. These methods can be used to set individual components of the date and time, such as the year, month, day, hour, minute, second, and millisecond, or to set the entire date and time at once.

setDate()

The setDate() method sets the day of the month for the Date object (1-31). The setDate() method takes a single parameter, which is the day of the month to set. If the specified day is outside of the valid range for the current month, the setDate() method will automatically adjust the month and year accordingly.

For example, the following code sets the day of the month for the current Date object to 15:

const now = new Date();
now.setDate(15);

The following code sets the day of the month for the Date object to 32, which will automatically adjust the month and year to the first day of the next month:

const nextMonth = new Date();
nextMonth.setDate(32);

setFullYear()

The setFullYear() method sets the year for the Date object. The setFullYear() method takes two parameters: the year to set and optionally the month to set. If the month parameter is omitted, the setFullYear() method will keep the current month.

For example, the following code sets the year for the current Date object to 2024:

const now = new Date();
now.setFullYear(2024);

The following code sets the year for the Date object to 2024 and the month to March:

const march2024 = new Date();
march2024.setFullYear(2024, 2);
// (March is the 3rd month)

setHours()

The setHours() method sets the hour of the day for the Date object (0-23). The setHours() method takes one parameter, which is the hour to set. If the specified hour is outside of the valid range, the setHours() method will automatically adjust the day accordingly.

For example, the following code sets the hour of the day for the current Date object to 15:

const now = new Date();
now.setHours(15);

setMinutes()

The setMinutes() method sets the minute of the hour for the Date object (0-59). The setMinutes() method takes one parameter, which is the minute to set. If the specified minute is outside of the valid range, the setMinutes() method will automatically adjust the hour accordingly.

For example, the following code sets the minute of the hour for the current Date object to 30:

const now = new Date();
now.setMinutes(30);

setSeconds()

The setSeconds() method sets the second of the minute for the Date object (0-59). The setSeconds() method takes one parameter, which is the second to set. If the specified second is outside of the valid range, the setSeconds() method will automatically adjust the minute accordingly.

For example, the following code sets the second of the minute for the current Date object to 45:

const now = new Date();
now.setSeconds(45);

setMilliseconds()

The setMilliseconds() method sets the millisecond of the second for the Date object (0-999). The setMilliseconds() method takes one parameter, which is the millisecond to set. If the specified millisecond is outside of the valid range, the setMilliseconds() method will automatically adjust the second accordingly.

For example, the following code sets the millisecond of the second for the current Date object to 500:

const now = new Date(); now.setMilliseconds(500);

setTime()

The setTime() method sets the time for the Date object in milliseconds since the Unix epoch (January 1, 1970 at 00:00:00 UTC). The setTime() method takes one parameter, which is the time in milliseconds to set.

For example, the following code sets the time for the current Date object to 12:00:00 UTC on January 1, 2024:

const now = new Date();
now.setTime(1672531200000);
// (milliseconds since the Unix epoch for January 1, 2024 at 12:00:00 UTC