JavaScript date setting methods
- Previous Page JS Date Getting Methods
- Next Page JS Math
Use the 'Set Date' method to set the date value of the date object (year, month, day, hour, minute, second, millisecond).
Date setting methods
Setting methods are used to set a specific part of the date. The following are the most commonly used methods (sorted in alphabetical order):
Method | Description |
---|---|
setDate() | Set the day (1-31) as a number |
setFullYear() | Set the year (optional month and day) |
setHours() | Set the hours (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): |
setFullYear() method
setFullYear()
Set the year of the date object. This example is set to 2020:
Example
<script> var d = new Date(); d.setFullYear(2020); document.getElementById("demo").innerHTML = d; </script>
setFullYear()
The method canSelectSet the month and day:
Example
<script> var d = new Date(); d.setFullYear(2020, 11, 3); document.getElementById("demo").innerHTML = d; </script>
setMonth() method
setMonth()
Set the month of the date object (0-11):
Example
<script> var d = new Date(); d.setMonth(11); document.getElementById("demo").innerHTML = d; </script>
setDate() method
setDate()
Set the day of the date object (1-31):
Example
<script> var d = new Date(); d.setDate(15); document.getElementById("demo").innerHTML = d; </script>
setDate()
The method can also be used to add daysAddTo date:
Example
<script> var d = new Date(); d.setDate(d.getDate() + 50); document.getElementById("demo").innerHTML = d; </script>
If you add days, switch months or years, the change will be automatically handled by the Date object.
setHours() method
setHours()
Set the hours of the date object (0-23):
Example
<script> var d = new Date(); d.setHours(22); document.getElementById("demo").innerHTML = d; </script>
setMinutes() method
setMinutes()
Set the minutes of the date object (0-59):
Example
<script> var d = new Date(); d.setMinutes(30); document.getElementById("demo").innerHTML = d; </script>
setSeconds() method
setSeconds()
Set the seconds of the date object (0-59):
Example
<script> var d = new Date(); d.setSeconds(30); document.getElementById("demo").innerHTML = d; </script>
Compare Dates
Dates can be easily compared.
The following example compares today with January 16, 2049:
Example
var today, someday, text; today = new Date(); someday = new Date(); someday.setFullYear(2049, 0, 16); if (someday > today) { text = "Today is before January 16, 2049"; } text = "Today is after January 16, 2049"; } document.getElementById("demo").innerHTML = text;
JavaScript counts months from 0 to 11. January is 0. December is 11.
Complete JavaScript Date Reference Manual
For a complete date reference manual, please visit our full JavaScript Date Reference Manual.
This manual includes descriptions and examples of all date properties and methods.
- Previous Page JS Date Getting Methods
- Next Page JS Math