views:

62

answers:

2

I have three selection drop downs in a form comprising of day, month, year. Pretty standard. Ive omitted all the individual select options for the purposes of this question.

<label for="start_date">Start Date<font class="required">*</font>:</label>
<select name="place_booking[day_val]">
<select name="place_booking[month_val]">
<select name="place_booking[year_val]">

Underneath this i have a selection for the number of days the client wishes to stay at the letting.

<label for="number_of_days">Number of Days<font class="required">*</font>:</label>
<select name="place_booking[number_of_days]">

Underneath there is a space to display the departure date based on there two selections above.

<label for="departure_date">Departure Date<font class="required">*</font>:</label>

? - this bit i would like to display the calculated date after the above is selected

Any help would be grealty appreciated.

A: 

You can use the code below

var start_date = new Date(parseInt(form['place_booking[year_val]'].value), parseInt(form['place_booking[month_val]'].value), parseInt(form['place_booking[day_val]'].value));
var departure_date = new Date(Date.parse(start_date) + 3600000*24*parseInt(form['place_booking[number_of_days]'].value));

or you can use the library located at http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_DateExtensions/Index.cfm which I have been using for my date operations in JavaScript. It's well documented and easy to use.

BYK
This idea looks very cool, i added this code to <script></script> in the head and added an input field with departure_date name. Was this the right thing to do? Its not playing ball.
Andy
Well this code is just to give an idea of how you can achive what you want with JavaScript. You have to give more details and code for me to write a simple "copy/paste" code for you or you have to know some JavaScript to adapt the code to your needs.
BYK
Thanks for your help BYK: Does this help: http://www.blackdownluxurylettings.co.uk/place_booking/2010-03-25,1
Andy
I tried adding an onblur just to see if it would update with the value. Im not very good at javascript sorry. Your help is greatly appreciated.
Andy
Give your form an id and replace the "form" in the code with document.getElementById('your_form_id_here')Then wrap this code in a function and assign this function to the "onchange" events of all the necessary selects and you should be ready to go.
BYK
OK cool, ill give it a go...thanks for your input.
Andy
HI BYK, I don't know whether you're around to see what i did based on your advice?
Andy
A: 

To calculate the date accurately you have to convert a d-m-y date to a number of days since some date in the past (the so-called Julian Day), add required number of days to it and then convert the result back to d-m-y.

Javascript has no built-in routine for this, but the formulae are well-known and already implemented on some sites (quick googling brought up this one among others).

stereofrog
Thanks for your advice stereofrog
Andy