2

I've used the following Javascript code to get the current year.

new Date().getFullYear()

what I want is to add three months to the current date and check whether the year changes! In java we can make this happen but I don't know how to do this javascript. How can I check this condition with javascript!

Imesh Chandrasiri
  • 5,428
  • 13
  • 56
  • 101
  • 3
    Look for accepted answer http://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript and just adjust number of months to be added. – Māris Kiseļovs May 27 '13 at 05:10

2 Answers2

5
var n1 = new Date().getFullYear();

var presentDate= new Date();
presentDate.setMonth(presentDate.getMonth()+No Of months you want to add);

var n2 = presentDate.getFullYear();

 if(n1 == n2)
    alert("Year not changed");
   else
    alert("Year changed");
PSR
  • 38,073
  • 36
  • 106
  • 149
1

to shift a date object to 3 months forward you can use the setMonth function.

var k = new Date();

k.setMonth(k.getMonth()+3);

alert(k);

Here is a demo fiddle.

Mithun Satheesh
  • 26,227
  • 14
  • 76
  • 99