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.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method sets the year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method can optionally set month and day.</p>
<p>Please note that month counts from 0. December is month 11:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setFullYear(2020, 11, 3);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMonth()</h2>
<p>The setMonth() method sets the mont of a date object.</p>
<p>Note that months count from 0. December is month 11:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setMonth(11);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setDate()</h2>
<p>The setDate() method sets the day of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setDate()</h2>
<p>The setDate() method can be used to add days to a date.</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setHours()</h2>
<p>The setHours() method sets the hours of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setHours(22);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMinutes()</h2>
<p>The setMinutes() function changes the minutes of a date object. The range for minutes is from 0 to 59.</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setMinutes(30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setSeconds()</h2>
<p>The setSeconds() method sets the seconds of a date object (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setSeconds(30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let text;
const today = new Date();
const someday = new Date();
someday.setFullYear(2100, 0, 14);
if (someday > today) {
text = "Today is before January 14, 2100.";
} else {
text = "Today is after January 14, 2100.";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>