How to use setFullYear in JavaScript

In this article, I will explain use of setFullYear() method in JavaScript date object.
  • 1846

JavaScript setFullYear() method

  • setFullYear() method sets the year of the date a given date according to local time.

  • setFullYear() method can also be used to set the month and day of month.

Syntax

Date.setFullYear(yearValue,monthValue,dayValue)

  • yearValue : A four-digit value representation the year. An integer specifying the numeric value of the year, for example, 2012.
  • monthValue : monthValue is the optional parameter. An integer between 0 and 11 representing the months January through December.
  • dayValue : dayValue is the optional parameter. An integer between 1 and 31 representing the day of the month. If you given the dayValue parameter, you must also given the monthValue.

Example

In the following example show to setFullYear() method set the date to six months ago.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Teal ">JavaScript setFullYear() Method Example</h3>

<p id="demo">Click for display the date six months ago.</p>

<button onclick="myFunction()">Click On</button>

<script type="text/javascript">

    function myFunction() {

        var d = new Date();

        d.setFullYear(d.getFullYear(), d.getMonth() - 6);

        var x = document.getElementById("demo");

        x.innerHTML ="Six Months Ago Date is : "+ d;

    }

</script>

</body>

</html>

Output1

setFullyear 1.jpg

Output2

setFullYear 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.