Add A Year To Today's Date
Asked Answered
S

10

136

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?

Schlep answered 11/10, 2015 at 21:44 Comment(3)
Possible duplicate of How to determine one year from now in JavascriptUpper
This is not a duplicate. Like I said above, unable to use standard javascript methodologySchlep
It might be helpful if you gave us some insight into what system this code is executed in.Whin
D
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

Dettmer answered 11/10, 2015 at 21:50 Comment(8)
There is no need to create two date objects. d.setFullYear(d.getFullYear() + 1) will do the job in much less code.Polysaccharide
but you will also lose the original dateDettmer
Then: var d = new Date(+originalDate) will copy the original date.Polysaccharide
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.Longford
@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.Adopt
@Longford 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.Theorem
@Longford see the updated solution to handle leap years.Vastitude
@Typhon: That might be expected, but it might be not. Depending on the business requirements, expected result might be Feb 28 2027 (last day of February the next year).Fluellen
W
159

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.

Whin answered 11/10, 2015 at 21:51 Comment(3)
You can use the moment npm library or date-fns if you'd like something a little lighter in dependency-weight.Reticule
@Reticule why use a library when vanilla JS can do the same job just as well?Bigg
If you add 1 year to 2020-02-29 it will give the output as 2021-03-01. However, this should be 2021-02-28.Vastitude
D
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

Dettmer answered 11/10, 2015 at 21:50 Comment(8)
There is no need to create two date objects. d.setFullYear(d.getFullYear() + 1) will do the job in much less code.Polysaccharide
but you will also lose the original dateDettmer
Then: var d = new Date(+originalDate) will copy the original date.Polysaccharide
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.Longford
@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.Adopt
@Longford 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.Theorem
@Longford see the updated solution to handle leap years.Vastitude
@Typhon: That might be expected, but it might be not. Depending on the business requirements, expected result might be Feb 28 2027 (last day of February the next year).Fluellen
D
35

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))
Dinadinah answered 28/2, 2019 at 16:32 Comment(0)
H
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)
Highpitched answered 24/9, 2018 at 20:29 Comment(0)
J
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)
)
Judejudea answered 25/2, 2020 at 7:30 Comment(3)
That is special. Can you break the passing of the new Date constructor down for usPettigrew
its just simply passing (new Date) object to an arrow functionJudejudea
That is not simply. That is some kind of magic that is not obvious to the casual passer by ;)Pettigrew
C
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);
Collettecolletti answered 18/12, 2018 at 12:59 Comment(0)
T
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
Thermal answered 2/1, 2019 at 4:51 Comment(0)
V
2

The solutions provided do not work with leap years and time zones.

Here is a solution that will handle both issues.

Date.UTC() is used to avoid time-zone time impact.

Rollover of the date to the next month in case of a leap year is handled with the last line of code.

Adding one (1) year to 2020-02-29 should give 2021-02-28 and not 2021-03-01 as other solutions suggest.

function addYears(date,years) {
date = new Date(date);
let day   = date.getDate(),
  newDate = new Date(Date.UTC(date.getFullYear()+years,date.getMonth(),date.getDate(),0));
newDate.getDate() != day && newDate.setDate(0);
return newDate;
}

//===== Test samples =====

console.log(addYears("2020-02-28",1));   // 2021-02-28  T00:00:00.000
console.log(addYears("2020-02-29",1));   // 2021-02-28  T00:00:00.000
console.log(addYears("2020-02-29",12));  // 2032-02-29  T00:00:00.000
console.log(addYears("2020-12-31",12));  // 2032-12-31  T00:00:00.000
console.log(addYears("2020-03-01",24));  // 2044-03-01  T00:00:00.000
console.log(addYears("2020-03-01",-5));  // 2015-03-01  T00:00:00.000
Vastitude answered 28/11, 2023 at 9:54 Comment(0)
S
-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);
    }
}
Soften answered 19/7, 2017 at 12:59 Comment(0)
P
-6
var yearsToAdd = 5;
var current = new Date().toISOString().split('T')[0];
var addedYears = Number(this.minDate.split('-')[0]) + yearsToAdd + '-12-31';
Procryptic answered 7/8, 2019 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.