How to change the Year in JavaScript?

In some cases, we need to change the date’s year in JavaScript.

We can do this pretty easily with setFullYear() method.

variable.setFullYear(year);

Example:

Change the year in “2022-04-01” date to 2010.

<div id="myDate"></div>

<script>
    myDate = new Date("2022-04-01");
    myDate.setFullYear('2010');

    document.getElementById('myDate').innerHTML = myDate;
</script>