[javascript] Add A Year To Today's Date

I am trying to add a year to todays date. I am working in a system that does not allow you to use standard JavaScript.

For instance, to get todays date I have to use:

javascript:now();

I have tried:

javascript:now(+1);

I have never seen this before, but am in need of adding one year to todays date...

Has anyone seen getting current date this way before? And if so, how could I add a year?

This question is related to javascript date

The answer is


This code adds the amount of years required for a date.

var d = new Date();
// => Tue Oct 01 2017 00:00:00 GMT-0700 (PDT)

var amountOfYearsRequired = 2;
d.setFullYear(d.getFullYear() + amountOfYearsRequired);
// => Tue Oct 01 2019 00:00:00 GMT-0700 (PDT)

var yearsToAdd = 5;
var current = new Date().toISOString().split('T')[0];
var addedYears = Number(this.minDate.split('-')[0]) + yearsToAdd + '-12-31';

Use the Date.prototype.setFullYear method to set the year to what you want it to be.

For example:

var aYearFromNow = new Date();
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1);

There really isn't another way to work with dates in JavaScript if these methods aren't present in the environment you are working with.


In Angular, This is how you Calculate Date

today = new Date();
year = this.today.getFullYear();
month = this.today.getMonth();
day = this.today.getDate();
//To go 18 years back
yearsBack18= new Date(this.year - 18, this.month, this.day);

//To go to same day next year
nextYear= new Date(this.year + 1, this.month, this.day);

One liner as suggested here

How to determine one year from now in Javascript by JP DeVries

new Date(new Date().setFullYear(new Date().getFullYear() + 1))

Or you can get the number of years from somewhere in a variable:

const nr_years = 3;
new Date(new Date().setFullYear(new Date().getFullYear() + nr_years))

    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();

    var fulldate = new Date(year + 1, month, day);

    var toDate = fulldate.toISOString().slice(0, 10);

    $("#txtToDate").val(toDate);

    output : 2020-01-02

//This piece of code will handle the leap year addition as well.

function updateExpiryDate(controlID, value) {
    if ( $("#ICMEffectiveDate").val() != '' &&
        $("#ICMTermYears").val() != '') {

        var effectiveDate = $("#ICMEffectiveDate").val();
        var date = new Date(effectiveDate);
        var termYears = $("#ICMTermYears").val();

        date = new Date(date.setYear(date.getFullYear() + parseInt(termYears)));
        var expiryDate = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
        $('#ICMExpiryDate').val(expiryDate);
    }
}

I like to keep it in a single line, you can use a self calling function for this eg:

If you want to get the timestamp of +1 year in a single line

_x000D_
_x000D_
console.log(_x000D_
  (d => d.setFullYear(d.getFullYear() + 1))(new Date)_x000D_
)
_x000D_
_x000D_
_x000D_

If you want to get Date object with single line

_x000D_
_x000D_
console.log(_x000D_
  (d => new Date(d.getFullYear() + 1, d.getMonth(), d.getDate()))(new Date)_x000D_
)
_x000D_
_x000D_
_x000D_


In case you want to add years to specific date besides today's date using either years, or months, or days. You can do the following

var christmas2000 = new Date(2000, 12, 25); 
christmas2000.setFullYear(christmas2000.getFullYear() + 5); // using year: next 5 years
christmas2000.setFullYear(christmas2000.getMonth() + 24); // using months: next 24 months
christmas2000.setFullYear(christmas2000.getDay() + 365); // using days: next 365 months

Function

var date = new Date();

var year = date.getFullYear();

var month = date.getMonth();

month = month + 1;

if (month < 10) {
    month = "0" + month;
} 

var day = date.getDate();

if (day < 10) {
    day = "0" + day;
} 

year = year + 1;

var FullDate =  year + '/' + month + '/' + day;