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!

Date Calculations 1

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi,

I have been trying to build a script to take a date that is going to be manually entered in to a text field and based on the selection from a dropdown add x amount of days to the date and display the value in the text field.

Well I’m ready to pull my hair out now as I have tried everything and was wondering if anyone could look at my code and point me in the right direction.

Thanks for looking

Code:
<html>

<head>
<script language="JavaScript">
<!--



//Get todays date in a format for the function below (poject start dates)
var d=new Date()
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()


//Takes the sales Stage and calulates the dates based on the stage.
function function1(Sales_Stage) {
    var Sales_Stage_value = (Sales_Stage.options[Sales_Stage.selectedIndex].text);
	//alert("Sales_Stage_value")
    
	if (Sales_Stage_value == "ASSESSED") {
	d.setDate(d.getDate()+4)
	document.forms[0]._course_start_date.value = d.getDay()+'/'+d.getMonth()+'/'+d.getYear()
	}	
	else if (Sales_Stage_value == "PLANNED") {
	d.setDate(d.getDate()+7)
	document.forms[0]._course_start_date.value = d.getDay()+'/'+d.getMonth()+'/'+d.getYear()
	}
	else if (Sales_Stage_value == "ACTIVE") {
	d.setDate(d.getDate()+5)
	document.forms[0]._course_start_date.value = d.getDay()+'/'+d.getMonth()+'/'+d.getYear()
	}
	

//	else if (Sales_Stage == "") {
//	document.forms[0]._date.value = day+"/"+month+"/"+year
//	}


}

-->
</script>

</head>

<body>

<form>

<Select name="Sales_Stage" onChange="function1(Sales_Stage)">
	<option></option>
	<option>ACTIVE</option>
	<option>PLANNED</option>
	<option>ASSESSED</option>
</selecT>


<input type="text" name="_course_start_date" value="01/10/2009" />

</form>


</body>


</html>
 
Hi

You are initializing the d only once, when the page is loaded, then change it adding more and more days each time a new [tt]option[/tt] is selected. Is that intentional ?

Feherke.
 
I think that your date adding logic is correct but you were just using the wrong date functions for setting the value in the text field e.g. d.getDay() will return the day of the week not the actual date...so you should use d.getDate(). Similary d.getYear() is browser dependent, if you want 4 digit year, always use getFullYear().

See if this works for you. Also, it is a good practice to end your JS statements with a semi-colon :)

Code:
<html>

<head>
<script language="JavaScript">
<!--
//Takes the sales Stage and calulates the dates based on the stage.
function function1(Sales_Stage) {
	//Get todays date in a format for the function below (poject start dates)
	var d=new Date();
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	
    var Sales_Stage_value = (Sales_Stage.options[Sales_Stage.selectedIndex].text);
    //alert("Sales_Stage_value")
    
    if (Sales_Stage_value == "ASSESSED") {
		d.setDate(d.getDate()+4);
		document.forms[0]._course_start_date.value = d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear();
    }    
    else if (Sales_Stage_value == "PLANNED") {
		d.setDate(d.getDate()+7);
		document.forms[0]._course_start_date.value = d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear();
    }
    else if (Sales_Stage_value == "ACTIVE") {
		d.setDate(d.getDate()+5);
		document.forms[0]._course_start_date.value = d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear();
    }
    

//    else if (Sales_Stage == "") {
//    document.forms[0]._date.value = day+"/"+month+"/"+year
//    }
}

-->
</script>

</head>

<body>

<form>

<Select name="Sales_Stage" onChange="function1(Sales_Stage)">
    <option></option>
    <option>ACTIVE</option>
    <option>PLANNED</option>
    <option>ASSESSED</option>
</selecT>


<input type="text" name="_course_start_date" value="01/10/2009" />

</form>


</body>


</html>

Nitin
 
Thanks for your replies i still can't get it working. Having looked at what i need i think this example will beter explain. The user can enter any date, i just defaulted this to use as an example. The problem i see is the changing the month or even year if the days run over the 30/31 st of a month, the=is example, a new one i have done just adds the days and doesn't change the month.

So the user here enters 01/10/2009 and they select ASSESSED from the drop down which is plus 32 days, i need it to change the date to the 02/11/2009 but it actually sets it to 32/10/2009.

ope that's clear and someone can guide me on this.

Code:
<html>

<head>


<script language="JavaScript">
<!--
//Takes the sales Stage and calulates the dates based on the stage.
function function1(Sales_Stage) {

//Gets date enteered by user
	var user_date = document.forms[0]._course_start_date.value;
		//alert(user_date);
	var set_user_date = new Date(user_date);

//Get the value selected from the dropdown
	var Sales_Stage_value = (Sales_Stage.options[Sales_Stage.selectedIndex].text);    

// if criteria matched then add 32 days to the user_date    
    if (Sales_Stage_value == "ASSESSED") {
		alert((set_user_date.getMonth()+32)+"/"+set_user_date.getDate()+"/"+set_user_date.getYear());
    }
	
// else if criteria matched then add 7 days to the user_date   
    else if (Sales_Stage_value == "PLANNED") {
		alert((set_user_date.getMonth()+8)+"/"+set_user_date.getDate()+"/"+set_user_date.getYear());
    }
	
// else if criteria matched then add 5 days to the user_date   
    else if (Sales_Stage_value == "ACTIVE") {
	alert((set_user_date.getMonth()+5)+"/"+set_user_date.getDate()+"/"+set_user_date.getYear());
    }
 
}

-->
</script>

</head>

<body>

<form>

<Select name="Sales_Stage" onChange="function1(Sales_Stage)">
    <option></option>
    <option>ACTIVE</option>
    <option>PLANNED</option>
    <option>ASSESSED</option>
</selecT>


<input type="text" name="_course_start_date" value="01/10/2009" />

</form>


</body>


</html>
 
Hi

JavaScript:
[b]var[/b] dayspan[teal]=[/teal][teal]{[/teal]
  [green][i]'ASSESSED'[/i][/green][teal]:[/teal][purple]32[/purple][teal],[/teal]
  [green][i]'PLANNED'[/i][/green][teal]:[/teal][purple]8[/purple][teal],[/teal]
  [green][i]'ACTIVE'[/i][/green][teal]:[/teal][purple]5[/purple]
[teal]}[/teal]

[b]function[/b] [COLOR=darkgoldenrod]function1[/color][teal]([/teal]Sales_Stage[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] user_date [teal]=[/teal] document[teal].[/teal]forms[teal][[/teal][purple]0[/purple][teal]].[/teal]_course_start_date[teal].[/teal]value[teal];[/teal]
  [b]var[/b] set_user_date [teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal]user_date[teal]);[/teal]

  [b]var[/b] Sales_Stage_value [teal]=[/teal] Sales_Stage[teal].[/teal]options[teal][[/teal]Sales_Stage[teal].[/teal]selectedIndex[teal]].[/teal]text[teal];[/teal]
  set_user_date[teal].[/teal][COLOR=darkgoldenrod]setDate[/color][teal]([/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getDate[/color][teal]()+[/teal]dayspan[teal][[/teal]Sales_Stage_value[teal]]);[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getMonth[/color][teal]()+[/teal][purple]1[/purple][teal]+[/teal][green][i]"/"[/i][/green][teal]+[/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getDate[/color][teal]()+[/teal][green][i]"/"[/i][/green][teal]+[/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getFullYear[/color][teal]());[/teal]
[teal]}[/teal]

Feherke.
 
Thanks for that!! It is nearly perfect. The only issue I have now and you will see in the example, is that I had it set to month/day/year and I need it day/month/year dd/mm/yyyy but when I change the date and the month round it kind of work but it concatinates and doesn’t add.

Really appreciate this, I know went searching for help on this before so many people struggle with it.

Code:
<html>

<head>


<script language="JavaScript">
<!--

var dayspan={
  'ASSESSED':32,
  'PLANNED':8,
  'ACTIVE':5
}

function function1(Sales_Stage)
{
  var user_date = document.forms[0]._course_start_date.value;
  var set_user_date = new Date(user_date);
 
  var Sales_Stage_value = Sales_Stage.options[Sales_Stage.selectedIndex].text;
 

 set_user_date.setDate(set_user_date.getDate()+dayspan[Sales_Stage_value]);
 
  alert(set_user_date.getDate()+"/"+set_user_date.getMonth()+1+"/"+set_user_date.getFullYear());
}

-->
</script>

</head>

<body>

<form>

<Select name="Sales_Stage" onChange="function1(Sales_Stage)">
    <option></option>
    <option>ACTIVE</option>
    <option>PLANNED</option>
    <option>ASSESSED</option>
</selecT>


<input type="text" name="_course_start_date" value="01/10/2009" />

</form>


</body>


</html>
 
Hi

You mean, the default 01/10/2009, which currently is interpreted as January 10, 2009 should be interpreted as 1 October 2009 ?

JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]function1[/color][teal]([/teal]Sales_Stage[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] user_date [teal]=[/teal] document[teal].[/teal]forms[teal][[/teal][purple]0[/purple][teal]].[/teal]_course_start_date[teal].[/teal]value[teal];[/teal]
  [highlight][b]var[/b] datepart [teal]=[/teal] user_date[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]'/'[/i][/green][teal]);[/teal][/highlight]
  [b]var[/b] set_user_date [teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][highlight][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]datepart[teal][[/teal][purple]2[/purple][teal]],[/teal][purple]10[/purple][teal]),[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]datepart[teal][[/teal][purple]1[/purple][teal]],[/teal][purple]10[/purple][teal])-[/teal][purple]1[/purple][teal],[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]datepart[teal][[/teal][purple]0[/purple][teal]],[/teal][purple]10[/purple][teal])[/teal][/highlight][teal]);[/teal]

  [b]var[/b] Sales_Stage_value [teal]=[/teal] Sales_Stage[teal].[/teal]options[teal][[/teal]Sales_Stage[teal].[/teal]selectedIndex[teal]].[/teal]text[teal];[/teal]

  set_user_date[teal].[/teal][COLOR=darkgoldenrod]setDate[/color][teal]([/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getDate[/color][teal]()+[/teal]dayspan[teal][[/teal]Sales_Stage_value[teal]]);[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getDate[/color][teal]()+[/teal][green][i]"/"[/i][/green][teal]+[highlight]([/highlight][/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getMonth[/color][teal]()+[/teal][purple]1[/purple][teal][highlight])[/highlight]+[/teal][green][i]"/"[/i][/green][teal]+[/teal]set_user_date[teal].[/teal][COLOR=darkgoldenrod]getFullYear[/color][teal]());[/teal]
[teal]}[/teal]

Feherke.
 
Perfect! Thanks a lot that got it. Great help and i am sure many people will find thi helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top