Code
Code

Reputation: 2129

Add A Year To Today's Date

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

For instance, to get today's 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 today's date...

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

Upvotes: 140

Views: 285739

Answers (11)

isapir
isapir

Reputation: 23483

It depends on how you define a year. If you don't care too much about leap years, and simply want to add 365 days, you can do it like so:

var futureDate = new Date(Date.now() + (86400 * 1000 * 365));

console.log(futureDate);

In ECMAScript you can make it easier to read using thousand separators:

    var futureDate = new Date(Date.now() + (86_400_000 * 365));

Upvotes: 0

Mohsen Alyafei
Mohsen Alyafei

Reputation: 5537

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

Upvotes: 3

Jon Koops
Jon Koops

Reputation: 9261

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.

Upvotes: 166

DC-
DC-

Reputation: 807

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)
)

Upvotes: 6

Jonah Williams
Jonah Williams

Reputation: 21421

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

Upvotes: 140

mluis
mluis

Reputation: 223

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

Upvotes: -6

Szekelygobe
Szekelygobe

Reputation: 2667

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))

Upvotes: 35

Md Shahriar
Md Shahriar

Reputation: 2736

    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

Upvotes: 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);

Upvotes: 2

LuscaDev
LuscaDev

Reputation: 359

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)

Upvotes: 11

Vishal Goel
Vishal Goel

Reputation: 1

//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);
    }
}

Upvotes: -3

Related Questions