JavaScript Set Date Methods


Set Date methods are used for setting a part of a date:

Method Description
setDate() Set the day as a number (1-31)
setFullYear() Set the year (optionally month and day)
setHours() Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth() Set the month (0-11)
setSeconds() Set the seconds (0-59)
setTime() Set the time (milliseconds since January 1, 1970)

The setFullYear() Method

The setFullYear() function adjusts the year of a date object. For instance, it can be used to change the year to 2020 in the following way:

The setFullYear() function has the option to set both the month and day.


The setMonth() Method

The setMonth() function changes the month of a date object, with values from 0 to 11.


The setDate() Method

The setDate() function is used to choose the day of the month for a date object, and it can be any number from 1 to 31.

The setDate() function can be employed to increase the number of days in a date.

If adding days shifts the month or year, the changes are handled automatically by the Date object.


The setHours() Method

The setHours() function allows you to adjust the hours of a date, ranging from 0 to 23.


The setMinutes() Method

The setMinutes() function adjusts the minutes of a date object, ranging from 0 to 59.


The setSeconds() Method

The setSeconds() function adjusts the seconds of a date object, ranging from 0 to 59.


Compare Dates

Dates can easily be compared.

In this example, we're checking how today's date compares to January 14, 2100.

JavaScript counts months from 0 to 11. January is 0. December is 11.