127

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?

Jazimov
  • 12,626
  • 9
  • 52
  • 59
Code
  • 1,969
  • 3
  • 18
  • 33
  • 2
    Possible duplicate of [How to determine one year from now in Javascript](http://stackoverflow.com/questions/8609261/how-to-determine-one-year-from-now-in-javascript) – rnevius Oct 11 '15 at 21:46
  • This is not a duplicate. Like I said above, unable to use standard javascript methodology – Code Oct 11 '15 at 21:48
  • 3
    It might be helpful if you gave us some insight into what system this code is executed in. – Jon Koops Oct 11 '15 at 21:57

9 Answers9

151

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

For example:

const aYearFromNow = new Date();
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1);
console.log(aYearFromNow);

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.

Jon Koops
  • 8,801
  • 6
  • 29
  • 51
139

You can create a new date object with todays date using the following code:

var d = new Date();
    console.log(d);
// => Sun Oct 11 2015 14:46:51 GMT-0700 (PDT)

If you want to create a date a specific time, you can pass the new Date constructor arguments

 var d = new Date(2014);
    console.log(d)
// => Wed Dec 31 1969 16:00:02 GMT-0800 (PST)

If you want to take todays date and add a year, you can first create a date object, access the relevant properties, and then use them to create a new date object

var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();
    var c = new Date(year + 1, month, day);
    console.log(c);
// => Tue Oct 11 2016 00:00:00 GMT-0700 (PDT)

You can read more about the methods on the date object on MDN

Date Object

Hitesh Tripathi
  • 856
  • 1
  • 11
  • 23
Jonah Williams
  • 20,499
  • 6
  • 65
  • 53
  • 90
    There is no need to create two date objects. `d.setFullYear(d.getFullYear() + 1)` will do the job in much less code. – RobG Oct 11 '15 at 23:01
  • 3
    but you will also lose the original date – Jonah Williams Oct 11 '15 at 23:11
  • Then: `var d = new Date(+originalDate)` will copy the original date. – RobG Oct 11 '15 at 23:12
  • 11
    This does not work with leap year date. e.g. if you enter 29/02/2016 after adding a year it will return 29/02/2017 which is incorrect. – Renascent Feb 03 '17 at 12:21
  • 7
    @ANP no, it returns 01/03/2017, which is probably still not what was required but it definitely will not return the non-existent date 29/02/2017. Keep in mind that in JavaScript, February is 1, not 2. – Roman Starkov Mar 24 '17 at 10:26
  • @Renascent it is working with leap year, at least with current browser versions. `let d = new Date('02/29/2016');` gives `Mon Feb 29 2016 00:00:00`. `d.setFullYear(d.getFullYear() + 1);` gives `Wed Mar 01 2017 00:00:00`. Which is what you would expect. – Typhon Jan 12 '21 at 12:56
33

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))
Szekelygobe
  • 2,309
  • 1
  • 19
  • 25
10

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)
LuscaDev
  • 329
  • 3
  • 8
5

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

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

If you want to get Date object with single line

console.log(
  (d => new Date(d.getFullYear() + 1, d.getMonth(), d.getDate()))(new Date)
)
DC-
  • 797
  • 9
  • 14
2

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);
2
    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
Md Shahriar
  • 2,072
  • 22
  • 11
-3

//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);
    }
}
-6
var yearsToAdd = 5;
var current = new Date().toISOString().split('T')[0];
var addedYears = Number(this.minDate.split('-')[0]) + yearsToAdd + '-12-31';
mluis
  • 213
  • 2
  • 13