Solved

Need to add days from dropdown to current date in jquery

Posted on 2015-01-11
2
199 Views
Last Modified: 2015-01-12
I need to display a  current date and add days from dropdown.

<SELECT class="QTY">
<OPTION VALUE="1">1</OPTION>
<OPTION VALUE="2">2</OPTION>
<OPTION VALUE="3">3</OPTION>
</SELECT>

 <INPUT TYPE=SUBMIT id='Button' VALUE="Go">   
     <input id='checkin' />
     <input id='checkout' />
     <input id='duration' />

$('#Button').click(function(){
    var checkin = new Date();
    var duration = $('.QTY option:selected').val();
    var checkout = new Date();
    
    checkout.setDate(checkin.getDate() + duration); 
    
	$('#checkin').val(checkin); 
    $('#checkout').val(checkout)  
    $('#duration').val(duration)
});

Open in new window

0
Comment
Question by:ucohockey
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40543857
Try this instead:
    var checkout = new Date(checkin);
    checkout.setDate(checkout.getDate() + Number(duration));

Open in new window

this copies the checkin date, then increases the day of the month by duration days, making sure it's converted from a string to a number. If it passes the end of the month, the Date object will take care of that. I made another fiddle: http://jsfiddle.net/robert_schutt/hpazp71v/
0
 

Author Closing Comment

by:ucohockey
ID: 40543861
Thank You much simpler  then what I came up with.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now