JavaScript setYear() Method

Definition and Usage

The setYear() method is used to set the year.



Parameter Description
year Required. A two or four digit number that indicates the year

Tips and Notes

Note: If the year parameter is a two-digit number, like setYear(91), it will be perceived as 1991. To specify a year before 1900 or after 1999, always use four digits!
Note: This method is always used in conjunction with a Date object.
Important: The setYear() method should no longer be used. Use the setFullYear() method instead!!


In this example we set the year to 1891 with the setYear() method:

<script type="text/javascript">
var d = new Date();

The output of the code above will be:

Mon May 11 1891 14:45:45 GMT+0530 (India Standard Time)
JavaScript setYear() Method Reviewed by 1000sourcecodes on 02:54 Rating: 5
Powered by Blogger.