homepage Welcome to WebmasterWorld Guest from 54.197.15.196
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript date function
problem with form
dawlish

10+ Year Member



 
Msg#: 55 posted 11:22 pm on Mar 3, 2002 (gmt 0)

I'm using the script below to get the date and using it to populate some select boxes on a booking enquiry form.
Arrival month & date
Departure month & date

var now = new Date();
var day = now.getDate();
var month = now.getMonth(); // numbered from 0
var day2 = now.getDate();
var month2 = now.getMonth(); // numbered from 0
function setDrops(frm_Search) {
// I assume Day and months are all there and start with "Select a Day and Select a month
frm_Search.arrivalDay.selectedIndex = day;
frm_Search.arrivalMonth.selectedIndex = month;
frm_Search.departureDay.selectedIndex = day2;
frm_Search.departureMonth.selectedIndex = month2;
}

I use the following html on the page for the select boxes

<b>Arrival</b>
<select name="arrivalMonth" size="1" onchange="amadChange(document.frm_Search.arrivalMonth, document.frm_Search.arrivalDay, document.frm_Search.departureMonth, document.frm_Search.departureDay)">
<option value="0">January<option value="1">February<option value="2">March<option value="3">April<option value="4">May<option value="5">June<option value="6">July<option value="7">August<option value="8">September<option value="9">October<option value="10">November<option value="11">December
</select>
<select name="arrivalDay" size="1" onchange="amadChange(document.frm_Search.arrivalMonth, document.frm_Search.arrivalDay, document.frm_Search.departureMonth, document.frm_Search.departureDay)">
<option value="1">1<option value="2">2<option value="3">3<option value="4">4<option value="5">5<option value="6">6<option value="7">7<option value="8">8<option value="9">9<option value="10">10<option value="11">11<option value="12">12<option value="13">13<option value="14">14<option value="15">15<option value="16">16<option value="17">17<option value="18">18<option value="19">19<option value="20">20<option value="21">21<option value="22">22<option value="23">23<option value="24">24<option value="25">25<option value="26">26<option value="27">27<option value="28">28<option value="29">29<option value="30">30<option value="31">31
</select>

<b>Departure</b>

<select name="departureMonth" size="1" onchange="dmddChange(document.frm_Search.departureMonth,document.frm_Search.departureDay)">
<option value="0">January<option value="1">February<option value="2">March<option value="3">April<option value="4">May<option value="5">June<option value="6">July<option value="7">August<option value="8">September<option value="9">October<option value="10">November<option value="11">December
</select>
<select name="departureDay" size="1" onchange="dmddChange(document.frm_Search.departureMonth,document.frm_Search.departureDay)">
<option value="1">1<option value="2">2<option value="3">3<option value="4">4<option value="5">5<option value="6">6<option value="7">7<option value="8">8<option value="9">9<option value="10">10<option value="11">11<option value="12">12<option value="13">13<option value="14">14<option value="15">15<option value="16">16<option value="17">17<option value="18">18<option value="19">19<option value="20">20<option value="21">21<option value="22">22<option value="23">23<option value="24">24<option value="25">25<option value="26">26<option value="27">27<option value="28">28<option value="29">29<option value="30">30<option value="31">31
</select>

This works fine in that is populates the select boxes with the current month and day:

Arrival March 03
Departure March 03

However I want the default for the departure date today + 2 days (e.g March 05).

I've tried changing var day2 = now.getDate(); to var day2 = now.getDate()+2;

This works to an extent until the end of the month where the month field will not be updated and the day field ends up being null as it would be 33 if the current day was 31.

How can I change the script so the departure date is +2 from the current.

I would be grateful for any advice and I'm a novice with Javascript.

 

txbakers

WebmasterWorld Senior Member txbakers us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 55 posted 11:36 pm on Mar 3, 2002 (gmt 0)

A great start.

No problem to catch the end of the month and roll it over, just takes more code.

I'm writing in psuedo-code to save space here.

if (month == 0 ) {
if (date + 2 == 33 ) {
date2 = 2;
month = 1;
}
}

you can build a simple array of 31 day months, or just write a longer routine to test each month individually.

Basically you test the date + 2 before you populate it, then make adjustments.

Good luck with it.

There are also great pop-up javascript calendars available that the user can click a date to populate a text box.

Let me know if you want one.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved