Date setUTCFullYear() Method - Javascript Date

Javascript examples for Date:setUTCFullYear

Description

The setUTCFullYear() method sets the year in four digits for dates between year 1000 and 9999, according the UTC time.

UTC time is the same as GMT time.

Syntax

date.setUTCFullYear(year, month, day)

Parameter Values

Parameter Description
year Required. A value representing the year, negative values are allowed
month Optional. An integer representing the month
day Optional. An integer representing the day of month

month expected values are 0-11, but other values are allowed:

  • -1 sets the last month of the previous year
  • 12 sets the first month of the next year
  • 13 sets the second month of the next year

day expected values are 1-31, but other values are allowed:

  • 0 sets the last hour of the previous month
  • -1 sets the hour before the last hour of the previous month
  • If the month has 31 days: 32 sets the first day of the next month
  • If the month has 30 days: 32 sets the second day of the next month

Return Value

Type Description
Number Milliseconds between the date object and midnight January 1 1970

The following code shows how to display the date six months ago.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from ww  w.j  ava 2 s .c  o m
    var d = new Date();
    d.setUTCFullYear(d.getUTCFullYear(), d.getUTCMonth() - 6);
    document.getElementById("demo").innerHTML = d;
}
</script>

</body>
</html>

Related Tutorials