Solved

Need to add days from dropdown to current date in jquery

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

Suggested Solutions

Title # Comments Views Activity
When we use cache:false ajax? 1 33
HTML5 Get data in table rows 5 29
how can i make the array into an object? 4 31
how can i get the string decoded ? 2 13
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

773 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