Skip to content
Advertisements

JavaScript Date.setFullYear()

Why setFullYear ?

JavaScript Date The setFullYear () method is a built-in function. Its task is to set the year of the date. For example: We can turn 1998 into 1999. The method that provides this transformation: setFulYear (). If you do not specify the monthValue and dateValue parameters, the values ​​returned from the getMonth () and getDate () methods are used.

The error does not occur if we set an excess date to the SetFullYear () method. For example; 15. We entered the value. 1 year for 12; 15 – Performs 12 = 3. And the date equals the third month of the next year.

Syntax:

Date.setFullYear(yearValue[, monthValue[, dayValue]])

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

return:

  • It returns the new date with updated year which is set by setFullYear() function.

JavaScript setFullYear Examples

Example 1

Date The setFullYear () method can change the year of a specific date.

        var date = new Date('October 21, 1983 09:22:12');
        console.log(date.getFullYear());


        date.setFullYear(2019);
        console.log(date.getFullYear());

1983
2019


Example 2

Date The setFullYear () method can change the year of the current date.

        var date = new Date();
        console.log(date.getFullYear());


        date.setFullYear(4125);
        console.log(date.getFullYear());

2019
4125


Example 3

        var date = new Date();
        console.log(date.getFullYear());


        date.setFullYear(2022);
        let year = date.getFullYear()
        let month = date.getMonth()
        console.log("year: " + year);
        console.log("month: " + month);

2019
year: 2022
month: 1


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: