开发者

Calculating a date when a date has been chosen by the number of days

开发者 https://www.devze.com 2022-12-23 15:09 出处:网络
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.

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"&开发者_C百科gt;*</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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号