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

Trouble creating a form from dates & maintaining form values 1

Status
Not open for further replies.

ITadvice

IS-IT--Management
Jul 22, 2008
38
0
0
US
I want to create a web form that automatically has the current date and the previous date as the default. Basically I want the form to have the last 24 hrs as the default date range for the form. It was suggested that I use javascript. I dont know much about javascript so this is what I have so far:

Code:
datecount=1;
var cur_dat=new Date();
curdate=cur_dat.getDate();
var diff_dat=new Date();
diff_dat.setDate(curdate-datecount);

My problem is that I don't know how to generate a dropdown menu from this information. My current HTML code looks like this:
Code:
<select name="month">
<option value="01">January</option>

<select name="day">
<option value="01">1</option>

<select name="year">
<option value="08">2008</option>

I want to keep the values I set because they are in the format needed to create reports from the form. How do I automatically set the current and previous date while still maintaining the values needed for the reports?

 

Another question, how effective would a cronjob be at accomplishing this? Is there a way to change the default dates on a daily basis? That way I could leave the html code as it is and just update the dates displayed. Any thoughts on this?
 
This will create a new date object 1 day before 'today':

Code:
var msecsInOneDay = 1000 * 60 * 60 * 24;
var today = new Date();
var yesterday = new Date(today.getTime() - msecsInOneDay);

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 

Thanks Dan, but that's not where I'm having trouble. My problem is that I don't know how to make a drop down menu that has today's date and yesterday's date pre-entered on the form. I want to keep the values I have for each selection so I can still be able to pass them on to my program. (Refer to my HTML sample above)
 
See if this gets you started. There is probably a better way to do this that I don't know.

Code:
<html>

<head>

<script type="text/javascript">

var d = new Date();

var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

var m = month[d.getMonth()];
var dd = d.getDate();
var y = d.getFullYear();

var msecsInOneDay = 1000 * 60 * 60 * 24;
var today = new Date();
var yesterday = new Date(today.getTime() - msecsInOneDay);

</script>

</head>

<body>

<p>The date is 
<script type="text/javascript">
document.write(m);
document.write(" ");
document.write(dd);
document.write(", ");
document.write(y);
</script>
</p>

<p>Yesterday was 
<script type="text/javascript">
document.write(yesterday);
</script>
</p>

<form action="">
<select name="month">
<script type="text/javascript">
document.write("<option value='");
document.write("'>");
document.write(m);
document.write("</option>");
</script>
</select>
</form>

</body>
</html>
 
I left a document.write(m); out as the second statement of the javascript in the form.
 

Thanks BigRed that was a BIG help. Sorry for not following up sooner.
 
No biggee. Glad to hear you are moving in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top