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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Down List Generate by Current Date 2

Status
Not open for further replies.

DebbieC

Programmer
Mar 29, 2001
168
0
0
US
I have a monthly report and the user needs to select the report they want according to the month and year. The user will generally be wanting to view the previous month's report but they might want an older one. The first report generated was for October 2006 but when 2007 comes real soon I don't want to hard code the year 2007 in the list. I need it to generate a new year every year starting with 2006.

Here is what I have for defaulting the Month's drop down list to the previous month:

Code:
<script language="JavaScript">
<!--
function initdt(mf) {
var t = new Date;
mf.month.value = t.getMonth();
mf.year.value = t.getFullYear();
} 
//-->
</script>

<body onload="initdt(document.DateList);">

	<select name="month" size="1">
	<option value="01">January</option>
	<option value="02">February</option>
	<option value="03">March</option>
	<option value="04">April</option>
	<option value="05">May</option>
	<option value="06">June</option>
	<option value="07">July</option>
	<option value="08">August</option>
	<option value="09">September</option>
	<option value="10">October</option>
	<option value="11">November</option>
	<option value="12">December</option>
	</select>&nbsp;&nbsp;

	<select size="1" name="year">
	<option value="2006">2006</option>
	<option value="2007">2007</option>
	</select>

But I need to add the code to generate the current year (if it's not 2006) and include all years from the current year to 2006 without hardcoding it like I did above. In other words, in 2008 it will list 2006, 2007 & 2008.


Thanks.

 
[tt]
function initdt(mf) {
var t = new Date;
mf.month.value = t.getMonth()[red]+1[/red];
[red]//[/red]mf.year.value = t.getFullYear();
[blue]mf.year.length=0
var nyear=t.getFullYear();
while (nyear>=[highlight]2006[/highlight]) { //lower limit of years
var ooption=document.createElement("option");
mf.year.options.add (ooption,0);
ooption.value=nyear;
ooption.text=nyear;
nyear--;
}[/blue]
}
[/tt]
 
Thank you soooooooo much! It works!!!
 
Well now that it's January I'm able to see if this works and it doesn't work quite right. It does for the year (it shows 2006 & 2007 in the dropdown list). However it's supposed to default to December 2006. In December it worked to default to November 2006 I guess the new year throws it off. It's supposed to default to the previous month because that's the report they are most likely to run.

Any ideas?
 
Use the setMonth method to set the value of t back one month, and then set the value of the year and month dropdowns equal to the month and year values for t:
(and always use proper indentation for your code, it makes it 10000000 times easier to debug)
Code:
function initdt(mf) {
   var t = new Date;
[s]   mf.month.value = t.getMonth()+1;
   //mf.year.value = t.getFullYear();[/s]
   mf.year.length=0
   var nyear=t.getFullYear();
   while (nyear>=2006) {  //lower limit of years
      var ooption=document.createElement("option");
      mf.year.options.add (ooption,0);
      ooption.value=nyear;
      ooption.text=nyear;
      nyear--;
   }
[!]   t.setMonth(t.getMonth() - 1);
   mf.month.value = t.getMonth()+1;
   mf.year.value = t.getFullYear();[/!]
}

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
It works great!!!!!!! Thanks!!!!!!
 
This was working until I added a link to a header. Now it defaults to January instead of December. When I remove the link to the header it works. I don't understand how that would affect it. I know that some javascripts interfere with letting others work however I can't see the code for the header because it's at the root of the directory and I don't have access to it. Here is the code:

Code:
<html>
	<head>
	<!--#include virtual="/_includes/header.inc"-->
		<title>Monthly Outpatient Reporting</title>
		<script language="JavaScript">
<!--

function initdt(mf) {
var t = new Date;
mf.year.length=0
var nyear=t.getFullYear();
while (nyear>=2006) {  //lower limit of years
var ooption=document.createElement("option");
mf.year.options.add (ooption,0);
ooption.value=nyear;
ooption.text=nyear;
nyear--;
}
   t.setMonth(t.getMonth() - 1);
   mf.month.value = t.getMonth()+1;
   mf.year.value = t.getFullYear();
} 

//-->
		</script>

I even tried putting the javascript code before the link to the header and it is the same.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top