JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
JavaScript Set Date Methods
Example
<!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>
Output
JavaScript setFullYear()
The setFullYear() method sets the year of a date object:
Wed Jul 08 2020 11:05:46 GMT+0530 (India Standard Time)
The setFullYear() Method
The setFullYear() method sets the year of a date object. In this example to 2020:
Example
<!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>
Output
JavaScript setFullYear()
The setFullYear() method sets the year of a date object:
Wed Jul 08 2020 11:06:48 GMT+0530 (India Standard Time)
The setFullYear() method is used optionally to set month and day:
Example
<!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>
Output
JavaScript setFullYear()
The setFullYear() method sets the year of a date object:
Wed Jul 08 2020 11:07:49 GMT+0530 (India Standard Time)
The setMonth() Method
The setMonth() method is used to set the month of a date object (0-11):
Example
<!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>
Output
JavaScript setMonth()
The setMonth() method sets the mont of a date object.
Note that months count from 0. December is month 11:
Thu Dec 08 2022 11:08:42 GMT+0530 (India Standard Time)
The setDate() Method
The setDate() method is used to set the day of a date object (1-31):
Example
<!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>
Output
JavaScript setDate()
The setDate() method sets the day of a date object:
Fri Jul 15 2022 11:09:22 GMT+0530 (India Standard Time)
The setDate() method adds days to a date:
Example
<!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>
Output
JavaScript setDate()
The setDate() method can be used to add days to a date.
Sat Aug 27 2022 11:10:06 GMT+0530 (India Standard Time)
The setHours() Method
The setHours() method is used to set the hours of a date object (0-23):
Example
<!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>
Output
JavaScript setHours()
The setHours() method sets the hours of a date object:
Fri Jul 08 2022 22:10:49 GMT+0530 (India Standard Time)
The setMinutes() Method
The setMinutes() method is used to set the minutes of a date object (0-59):
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMinutes()</h2>
<p>The setMinutes() method sets the minutes of a date object (0-59):</p>
<p id=”demo”></p>
<script>
const d = new Date();
d.setMinutes(30);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript setMinutes()
The setMinutes() method sets the minutes of a date object (0-59):
Fri Jul 08 2022 11:30:33 GMT+0530 (India Standard Time)
The setSeconds() Method
The setSeconds() method is used to set the seconds of a date object (0-59):
Example
<!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>
Output
JavaScript setSeconds()
The setSeconds() method sets the seconds of a date object (0-59):
Fri Jul 08 2022 11:12:30 GMT+0530 (India Standard Time)
Compare Dates
The following example compares today’s date with January 14, 2100:
Example
<!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>
Output
Today is before January 14, 2100.