setFullYear(year)
Sets the year of the date.
The year must be given with four digits (2007 instead of just 07).
This method can also be used to set the month and day of month.
setFullYear() |
Yes | Yes | Yes | Yes | Yes |
dateObject.setFullYear(year, month, day);
Parameter | Description |
---|---|
year | Required. A four-digit value representing the year, negative values are allowed |
month | Optional. An integer representing the month.
Expected values are 0-11, but other values are allowed. -1 will be the last month of the previous year |
day | Optional. An integer representing the day of month.
Expected values are 1-31, but other values are allowed.
0 will be the last day of the previous month -1 will be the day before the last day of the previous month If the month has 31 days: 32 will be the first day of the next month If the month has 30 days: 32 will be the second day of the next month |
return a number representing the number of milliseconds between the date object and midnight January 1 1970.
var myDate = new Date();
myDate.setFullYear(2012);
console.log(myDate.toString());
The code above generates the following result.
The following code sets the date to November 3, 2014:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w w w . j av a 2 s .co m-->
var d = new Date();
d.setFullYear(2014, 10, 3);
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code sets the date to 3 months ago:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!-- w w w . j a v a 2s . c om-->
var d = new Date();
d.setFullYear(d.getFullYear(), d.getMonth() - 3);
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
The code above is rendered as follows: