Solved

Need to add days from dropdown to current date in jquery

Posted on 2015-01-11
2
203 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

808 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