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!

Testing Dates 1

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi,

I need to test a dropdown and based on it's value take todays date and add a certain number of days to that date.


Here are the dropdown values and the number of days dependant on which is selected. See my code below.

Problem i have is this, what if the date is 30/07/2009 and i need to add 5 days on. The way i have it at the moment the result will look like 35/07/2009 which is no good i need it to work out the date logic and give methe result of 04/08/2009

Does anyone have any hints for this? Thanks in adavnce

Assessment = 2 days
Planned = 5 days
Active = 3 days


Code:
<script language="">
<!--
var today_date= new Date()
var month=today_date.getMonth()+1
var today=today_date.getDate()
var year=today_date.getFullYear()


function function1(Sales_Stage) {
    var Sales_Stage = (Sales_Stage.options[Sales_Stage.selectedIndex].text);
    if (Sales_Stage == "ASSESSMENT") {
	alert((today+2)+"/"+month+"/"+year);
	} 
	else if (Sales_Stage == "PLANNED") {

	alert((today+5)+"/"+month+"/"+year);
	}
	else if (Sales_Stage == "ACTIVE") {
    
	alert((today+3)+"/"+month+"/"+year);
	}
}
-->
</script>



<select name="_sales_stage" id="_sales_stage" class=ScheduleActivityType onChange=function1(this);  format=Text >
<option></option> 
<option>ACTIVE</option> 
<option>ASSESSMENT</option> 
<option selected>PLANNED</option> 
<option>REFERRAL</option> 
</select>
 
Hi

Use [tt]Date.setTime()[/tt] for such operations :
JavaScript:
[gray]// today + milliseconds in a second * seconds in a minute * minutes in an hour * hours in a day * days to add[/gray]
today_date[teal].[/teal][COLOR=darkgoldenrod]setTime[/color][teal]([/teal]today_date[teal].[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()+[/teal][purple]1000[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]24[/purple][teal]*[/teal][purple]5[/purple][teal])[/teal]

Feherke.
 
Or you could just add the number of days with setDate:
Code:
var d=new Date()
alert(d.toString())  //shows today's date
d.setDate(d.getDate()+9)
alert(d.toString()) //shows today's date plus 9 days

_________________
Bob Rashkin
 
Hi

Yepp. I think I should review my old habits. I vaguely remember that I had a problem with that some years ago. Probably with a browser that disappeared in meantime...

Feherke.
 
Thanks for all your help. Bong your example did the job perfectly. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top