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

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

3 Comments

thank you its work now. can you explain 'var curr_date = ("0" + (da.getDate())).slice(-2)' line
You can find more details here Nasik.stackoverflow.com/questions/6040515/…
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".
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());

3 Comments

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 .
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
try this: da.setMonth(da.getMonth + 12) instead of "da.setFullYear(da.getFullYear()+1, da.getMonth());" This will automatically increase the year also
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

1 Comment

if i increases the only the year the the out put 2013-1-29
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/

6 Comments

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 :It is the requirement you need which I understood from the question. Can you little more explain the question
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 @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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.