...

View Full Version : Comparing Dates in dynamic select box?



Tanker
07-26-2002, 06:00 PM
I have been asked to write a script that will take an array of dates in mm/dd/yyyy format, write them into a select box (has to be done with javascript because its subject to change without reloading the current page), then auto select todays date, or the date closest to it without going beyond todays date.

In the code below I have the date array hardcoded, it's actually passed back from a cold fusion query thats run in an iframe. This code works except its selecting tomorows date rather than yesterdays date when I pull out todays date. :(

Thanks in advance...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript1.2">
<!--
dateArray = new Array("10/26/2002","10/27/2002","10/28/2002","10/29/2002","10/30/2002","10/31/2002","07/29/2002","07/28/2002","07/27/2002","07/26/2002","07/25/2002","07/24/2002","07/23/2002","07/22/2002","07/19/2002","07/18/2002","07/10/2002","07/09/2002","07/08/2002","07/05/2002","07/03/2002","07/02/2002","07/01/2002","06/26/2002","06/24/2002","06/20/2002","06/13/2002","06/10/2002","03/01/2002");

function makeDates(){
myDate = new Date()
tMonth = myDate.getMonth()+1
if(tMonth <= 9){ curMonth = "0" + tMonth }else{ curMonth = tMonth }
if(myDate.getDate() <= 9){ curDay = "0" + myDate.getDate() }else{ curDay = myDate.getDate() }
curYear = myDate.getFullYear()
curDate = curMonth + "/" + curDay + "/" + curYear

for(i=0;i<dateArray.length;i++){
document.test.dateRange.options[i] = null
}
for(i=0;i<dateArray.length;i++){
if(dateArray[i].substr(0,2) >= curMonth){
if(dateArray[i].substr(3,2) >= curDay){
if(dateArray[i].substr(6,4) >= curYear){
document.test.dateRange.options[i] = new Option(dateArray[i],dateArray[i],true,true);
}
}
} else {
document.test.dateRange.options[i] = new Option(dateArray[i],dateArray[i],false,false);
}
}
}
//-->
</script>
</head>

<body>
<div align="center">
<form name="test">
<select name="dateRange"></select>
<br>
<input type="button" value="makedates" onClick="makeDates()">
<input type="button" value="ShowVal" onClick="alert(document.test.dateRange.options[document.test.dateRange.selectedIndex].text)"></form>
</div>
</body>
</html>

coxy
07-26-2002, 08:02 PM
Hey!

This is what I came up with:

<html>
<head>
<title>Making Dates</title>
<script language="JavaScript" type="text/JavaScript">
var d=new Array("10/26/2002","10/27/2002","10/28/2002","10/29/2002","10/30/2002","10/31/2002","07/29/2002","07/28/2002","07/27/2002","07/26/2002","07/25/2002","07/24/2002","07/23/2002","07/22/2002","07/19/2002","07/18/2002","07/10/2002","07/09/2002","07/08/2002","07/05/2002","07/03/2002","07/02/2002","07/01/2002","06/26/2002","06/24/2002","06/20/2002","06/13/2002","06/10/2002","03/01/2002");

function makeDates(){
var t,c=new Date().getTime(),b=document["test"]["dateRange"].options
for(var i=0;i<d.length;i++){b[i]=new Option(d[i])
t=new Date().getTime()-new Date(d[i]).getTime()
if(t<c&&t>=0){c=t;b[i].selected=true}
}
}
</script>
</head>

<body>
<FORM Name="test">
<SELECT Name="dateRange"></SELECT>
<input value="makedates" type="button" onClick="makeDates()">
</FORM>
</body>
</html>


Hope that helps,

coxy :)

Tanker
07-26-2002, 08:47 PM
Thanks , thats what I needed it to do. Took me a bit to figure out what variables I needed to replace with mine but it works perfectly now. Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum