JavaScript setFullYear Function

JavaScript setFullYear function is used to set the Year, Month, Day of a given date as per local time. The syntax of the setFullYear function is:

 Date.setFullYear(Year, Month_Number, Day_Number)

In this Date Function both the Month Number and Day Number arguments are optional.

JavaScript setFullYear Function Example

Here, we are using setFullYear to set the current year to 2014.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Full Year Functions </title>
</head>
<body>
    <h1> JavaScript setFullYear Function Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt + "<br/>");

  dt.setFullYear(2014);
  document.write("After setFullYear() : " + dt);
</script>
</body>
</html>
JavaScript setFullYear Example

Date and Time: Tue Nov 06 2018 12:20:54 GMT+0530 (Indian Standard Time)
After setFullYear() : Thu Nov 06 2014 12:20:54 GMT+0530 (Indian Standard Time)

JavaScript set Full Year Function Example 2

In this setFullYear example, we set the Full Year of a custom date to 2014, and Month to December.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Full Year Functions </title>
</head>
<body>
    <h1> JavaScript setFullYear Function Example </h1>
<script>
  var dt = Date("January 1, 2017 10:11:22");
  document.write("Date and Time : " + dt + "<br/>");

  dt.setFullYear(2014, 11);
  document.write("After setFullYear() : " + dt);
</script>
</body>
</html>
JavaScript setFullYear Function 2

In this JavaScript set Full Year example, we set the Year to 2014, Month to December, and Day to 31

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Full Year Functions </title>
</head>
<body>
    <h1> JavaScript setFullYear Function Example </h1>
<script>
  var dt = Date("January 1, 2017 10:11:22");
  document.write("Date and Time : " + dt + "<br/>");

  dt.setFullYear(2014, 11, 31);
  document.write("After setFullYear() : " + dt);
</script>
</body>
</html>
JavaScript setFullYear Example

Date and Time: Sun Jan 01 2017 10:11:22 GMT+0530 (Indian Standard Time)
After setFullYear() : Wed Dec 31 2014 10:11:22 GMT+0530 (Indian Standard Time)