Forum Moderators: open

Message Too Old, No Replies

comparing dates

         

franches

3:05 am on Nov 5, 2004 (gmt 0)

10+ Year Member



hi,
i have a problem with my code. If I tried to press December in my select box it does not display the message "Filtering in advance is not allowed!" Kindly help me with the code.

<script language="javascript">
var d = new Date()
tempFilMonth = d.getMonth() + 1;
tempFilYear = d.getFullYear();
tempFilDay = d.getDate();

function FilterLogDate()
{
FilterDateNow = tempFilYear +"-"+ tempFilMonth +"-"+ tempFilDay;
FilterTempLogDate = document.ViewForm.FilterYear.value +"-"+ document.ViewForm.FilterMonth.value +"-"+ document.ViewForm.DayChoice.value;

if (FilterTempLogDate > FilterDateNow)
{
("Filtering in advance is not allowed")
}
}
</script>
<form>
<select class=select id="FilterMonth" name="FilterMonth" style="width:12%;" onchange="FilterLogDate()">
<option value="1">January</option>
...
<option value="11">November</option>
<option value="12">December</option>
</select>

<select class=select name="FilterYear" style="width:8%;">
<? for ($iYear=strftime("%Y",(time()))-1; $iYear<=strftime("%Y",(time())); $iYear=$iYear+1)
{
?>
<option value="<? echo $iYear;?>"><? echo $iYear;?></option>
<? }?>
</select>
</form>

thanks

adni18

4:04 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First of all, you forgot to specify how javascript should display the message (I would guess an alert). You simply put ("Filtering in advance is not allowed"). Second, naturally, this wouldn't work, since Jscript cannot compare strings based on seperated numerical values.

I would try this:

<script language="javascript">
var d = new Date()
tempFilMonth = d.getMonth() + 1;
tempFilYear = d.getFullYear();
tempFilDay = d.getDate();
function FilterLogDate()
{
FilterDateNow = tempFilYear +"-"+ tempFilMonth +"-"+ tempFilDay;
FilterTempLogDate = document.ViewForm.FilterYear.value +"-"+ document.ViewForm.FilterMonth.value +"-"+ document.ViewForm.DayChoice.value;

splita=FilterTempLogDate.split("-");
splitb=FilterDateNow.split("-");
if (splita[0] > splitb[0] ¦¦ splita[1] > splitb[1])
{
alert("Filtering in advance is not allowed")
}
else if (splita[0] > splitb[0])
{
if(splita[1] > splitb[1] ¦¦ splita[2] > splitb[2])
{
alert("Filtering in advance is not allowed")
}
}
}
</script>
<form>
<select class=select id="FilterMonth" name="FilterMonth" style="width:12%;" onchange="FilterLogDate()">
<option value="1">January</option>
...
<option value="11">November</option>
<option value="12">December</option>
</select>

<select class=select name="FilterYear" style="width:8%;">
<? for ($iYear=strftime("%Y",(time()))-1; $iYear<=strftime("%Y",(time())); $iYear=$iYear+1)
{
?>
<option value="<? echo $iYear;?>"><? echo $iYear;?></option>
<? }?>
</select>
</form>

franches

11:48 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



thank you very much.

adni18

1:07 pm on Nov 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Additionally, you can't simply get the value of the selects; you have to access them as document.ViewForm.FilterMonth.options[document.ViewForm.FilterMonth.selectedIndex].value