Javascript Date setFullYear(year)

Introduction

Javascript Date setFullYear(year) sets the year of the date.

dateObj.setFullYear(yearValue[, monthValue[, dateValue]])

Parameters

  • yearValue - An integer specifying the numeric value of the year, for example, 1995.
  • monthValue - Optional. An integer between 0 and 11 representing the months January through December.
  • dateValue - Optional. An integer between 1 and 31 representing the day of the month. If you specify the dateValue parameter, you must also specify the monthValue.

Return value

The number of milliseconds between 1 January 1970 00:00:00 UTC and the updated date.

If a parameter specified is outside of the range, setFullYear() will update the other parameters accordingly.

For example, if you specify 15 for monthValue, the year is incremented by 1 (year value + 1), and 3 is used for the month.

The year must be given with four digits (2019 instead of just 19).

let d = new Date();
console.log(d);/*  w ww .  j  av  a 2 s . c o m*/
d.setFullYear(1997);
console.log(d);

Set the date to November 3, 2020:

var d = new Date();
d.setFullYear(2020, 10, 3);/*from ww w .j  a va2 s. c o  m*/
console.log(d);

Set the date to six months ago:

var d = new Date();
d.setFullYear(d.getFullYear(), d.getMonth() - 6);
console.log(d);/*from ww  w.jav  a2  s .  c o  m*/



PreviousNext

Related