Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dates and form fields

Status
Not open for further replies.

andyc209

IS-IT--Management
Dec 7, 2004
98
GB
I have a 3 form fields

<form name="datepick" id="datepick"....

<select name="dayselect" id="dayselect">
<option value="7">7</option>
<option value="14">7</option>
</select>
<input type="text" name="date1" id="date1" value="<% todaysdate %>">
<input type="text" name="date2" id="date1">
</form>

what i want to happen is when the user selects the number of days can the field called date2 be populated with the selected days to the date in date1.
i.e. if 7 is selected then 7 days are added to todays date.

thanks if anyone can help
 
Code:
<form name="datepick" id="datepick"....
<select name="dayselect" id="dayselect" [red][b]onChange="document.getElementById('date2').value=this.value;"[/b][/red]>
<option value="7">7</option>
<option value="14">[red][b]14[/b][/red]</option>
</select>
<input type="text" name="date1" id="date1" value="<%[red][b]=[/b][/red]todaysdate %>">
<input type="text" name="date2" id="[red][b]date2[/b][/red]">
</form>

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never in the 'loop' " - DreX 2005
 
You'll need to use the JS Date object to do this.

First, get the date in date1 as a Date object. You can either do this yourself, by breaking down the component parts of the date:

Code:
var date1Obj = new Date(year, month, day[, hours[, minutes[, seconds[, milliseconds]]]])

or you can try and get the Date object to do the work for you:

Code:
var date1Str = 'April 8, 2009 7:45 AM';
var date1Obj = Date.parse(date1Str);

Assuming you have this, by far the easiest way to add on an arbitrary number of days is to convert that number to milliseconds, do the addition, and let the Date object deal with any validation issues (e.g. adding 1 day on to the 30th of April will automatically give the you the 1st of May, not an invalid 31st of April).

To do this, find the number of milliseconds in 1 day (60 secs/min * 60 mins/hr * 24 hrs/day * 1000):

Code:
var millisInADay = 86400000;

then convert your date1Obj to millis:

Code:
var date1InMillis = date1Obj.getTime();

This is the handy part: the Date object constructor also takes a number of milliseconds, so now it's just a case of doing simple addition and multiplication:

Code:
var date2Obj = new Date(date1InMillis + (millisInADay * numDaysToAdd));

Combine this with Robert's script above to get and set the values, and you should be sorted.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
whoops thanks dan, i forgot to plug in the date math in the code snip

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never in the 'loop' " - DreX 2005
 
i am a bit rubbish with javascript, great with ASP stuff but my scripting is pants! Can you help me by showing me how to write this out (sorry for needing the help). I understand your logic and thanks for your input so far.
 
Can you help me by showing me how to write this out

No - because I have no idea of the format that 'todaysdate' is written out as. That's the problem with providing server-side code for a client-side problem :)

Have a bash at it yourself, anyhow, and post the code if you get stuck.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
not had chance to try it out yet,ran the london marathon yesterday so had abusy last week getting ready! Absolutely knackered! Will get back onto this later this week when recovered and will post back what happens. thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top