0

i need to add a year to given date. I have try this code

<script>
  function get_expir(){ 
     regDate = document.getElementById('txt1').value;         

     var da = new Date(regDate);
     da.setFullYear(da.getFullYear()+1, da.getMonth()+1);
     document.form1.txt2.value = da.getFullYear()+ '-'+ da.getMonth()+ '-'+ da.getDate();  
  }
</script

if the user input is '2012-02-29' then the output will comes '2013-2-29'. Blockquote

NASIK NSK
  • 55
  • 1
  • 11

4 Answers4

2

Try this :

var da = new Date(regDate);

da.setFullYear(da.getFullYear()+1);

var curr_date =  ("0" + (da.getDate())).slice(-2)
var curr_month = ("0" + (da.getMonth() + 1)).slice(-2)
var curr_year = da.getFullYear();

document.form1.txt2.value = curr_date + "-" + curr_month + "-" + curr_year;
Jaseem Abbas
  • 5,028
  • 6
  • 48
  • 73
  • thank you its work now. can you explain 'var curr_date = ("0" + (da.getDate())).slice(-2)' line – NASIK NSK Feb 17 '15 at 10:26
  • You can find more details here Nasik.http://stackoverflow.com/questions/6040515/how-do-i-get-month-and-date-of-javascript-in-2-digit-format – Jaseem Abbas Feb 17 '15 at 10:27
  • Let's take an example. If the month in `regDate` is Feb, `da.getMonth()` gives you 2. We append a 0 before `da.getMonth()` to get 2. ie, `"0" + da.getMonth()` will now give you `02`. `slice(-2)` says, take the last 2 characters of the given string. ie, `("02").slice(-2)` gives `"02"` itself. Now lets say, the month in `regDate` is Dec. `da.getMonth()` gives you `12`. We append a 0 before `da.getMonth()` to get `012`. ie, `"0" + da.getMonth()` will now give you `012`. `slice(-2)` says, take the last 2 characters of the given string. ie, `("012").slice(-2)` gives `"12"`. – Jaseem Abbas Feb 17 '15 at 11:16
0
 var da = new Date(regDate);
 da.setFullYear();
 da.setMonth();

I you want to format the output then:

(new Date(Date.UTC(da.getFullYear()+1, da.getMonth() + 1, da.getDay(), 0, 0, 0))).toISOString().replace(/T.*Z$/, '');

Notice that I made everything in one line but for clarity those steps should be on different lines

Raulucco
  • 3,406
  • 1
  • 21
  • 27
0

It would help if you are more clear in expaining the question .From what i understood , you want to increase the year but according to your code you are increasing the month as well. this is thee correction you need to make:

da.setFullYear(da.getFullYear()+1, da.getMonth());

psh1990
  • 19
  • 3
  • there is a registration panel in my system. when the registration date is selected, the registration expiry date should calculate automatically, so the expiry date will be 1 year from the register date. So i need to increment the year by 1 . – NASIK NSK Feb 17 '15 at 09:39
  • if i use this line "da.setFullYear(da.getFullYear()+1, da.getMonth());" the function increases only 11 month, Thats why i increasess the month too – NASIK NSK Feb 17 '15 at 09:43
  • try this: da.setMonth(da.getMonth + 12) instead of "da.setFullYear(da.getFullYear()+1, da.getMonth());" This will automatically increase the year also – psh1990 Feb 17 '15 at 09:51
0
var strDate =document.getElementById('txt1').value
var array = strDate .split('-');
var newDate= (parseInt(array[0])+1)+"-"+array[1]+"-"+array[2];

try this,you may refer this jsfiddle experimentation http://jsfiddle.net/z1rx1djf/

Arvin
  • 954
  • 3
  • 14
  • 33
  • just adding 1 to the year and month is not giving the correct output.if the input is 2012-02-29. it will out put 2013-02-29 – NASIK NSK Feb 17 '15 at 09:50
  • @NASIK NSK :It is the requirement you need which I understood from the question. Can you little more explain the question – Arvin Feb 17 '15 at 09:58
  • there is a registration panel in my system. when the registration date is selected, the registration expiry date should calculate automatically, so the expiry date will be 1 year from the register date. – NASIK NSK Feb 17 '15 at 10:01
  • so @NASIK NSK if you register on today 2015-2-17 the expiry date should be 2016-2-17 right? that is what happening here, Have you checked the jsfiddle – Arvin Feb 17 '15 at 10:03
  • yap, that what i need to be happen, but if today is 2012-2-29 then the out put 2013-2-30.how to solve this – NASIK NSK Feb 17 '15 at 10:10
  • if you use that ode in the js fiddle only year will add up. neither month nor date will add up. – Arvin Feb 17 '15 at 10:15