Solved

Need to add days from dropdown to current date in jquery

Posted on 2015-01-11
2
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Find out all repos that a user is most active on Github 1 30
Print a <div></div> only 5 39
Read text on Table 7 29
Can anyone help me find this preloader? 51 42
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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)

749 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