Javascript auto update date field for duration

Similar to: Javascript date function to calculate the end date using the duration

I have a website & need part of the form to autocalculate the end date (30 days from the entry date) after the month field has been entered.

The fields in use are StartDD, StartMM, StartYYYY.

At present I have the input text fields linked with auto clear & auto-hop
I.E
Start Date: (DD) - (MM) - (YYYY)
Jumping into DD will clear the DD text, typing in 2 numbers in DD will jump it into the MM box & so on.

At present the YYYY field is auto-populated however this may have to change at the end of the year.

Any helps with examples would be great
LVL 6
kiwistagAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AmickCommented:
<script type="text/javascript">
var d=new Date();
d.setFullYear(YYYY,MM-1,DD);
document.write(d,'<br \>');
d.setDate(d.getDate()+30);
document.write(d);

</script>
Note that month is zero based - 0=January
0
AmickCommented:
To make it easier, here's a function:
function addDays(YYYY,MM,DD,Days)
{
var d=new Date();
d.setFullYear(YYYY,MM-1,DD);
d.setDate(d.getDate()+Days);
return(d);
}

Open in new window


call it with
updateDate(YYYY,MM,DD,30) to add 30 days.
updateDate(2011,3,28,30) for example.

You can also enter a  negative number to get a date in the past.  

This function will work across year boundaries as well, so there's no need to worry about changing your code as you approach the end of the year.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmickCommented:
Cut and paste error above, in development I saw calling the function updateDate and changed it to addDays, before I pasted it in.  Clearly, the function addDays is called using the function name addDays rather than updateDate. I apologize for the error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.