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!

Date comparison Problem 2

Status
Not open for further replies.

barrylowe

Programmer
Nov 6, 2001
188
0
0
GB
Please note: I have posted this question in the ColdFusion fourm as well.

I am having real problems trying to compare a date generated from 3 coldfusion select boxes and the current date.

The 3 select boxes are generated using the following ColdFusion code:

<cfselect name=&quot;ComboDay&quot;>
<option value=&quot;&quot; selected>
<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;31&quot;>
<option value=&quot;#i#&quot;>#i#
</cfloop>
</cfselect>

<cfselect name=&quot;ComboMonth&quot;>
<option value=&quot;&quot; selected>
<option value=&quot;1&quot;>Jan
<option value=&quot;2&quot;>Feb
<option value=&quot;3&quot;>Mar
<option value=&quot;4&quot;>Apr
<option value=&quot;5&quot;>May
<option value=&quot;6&quot;>Jun
<option value=&quot;7&quot;>Jul
<option value=&quot;8&quot;>Aug
<option value=&quot;9&quot;>Sep
<option value=&quot;109&quot;>Oct
<option value=&quot;11&quot;>Nov
<option value=&quot;12&quot;>Dec
</cfselect>

<cfselect name=&quot;ComboYear&quot;>
<option value=&quot;&quot; selected>
<option value=&quot;2002&quot;>2002
<option value=&quot;2003&quot;>2003
<option value=&quot;2004&quot;>2004
<option value=&quot;2005&quot;>2005
<option value=&quot;2006&quot;>2006
</cfselect>

I have then written the following javascript function to ensure that the date selected is not in the past.

var ProposedDate = Form_NewSolution.ComboYear.value + &quot;-&quot; + Form_NewSolution.ComboMonth.value + &quot;-&quot; + Form_NewSolution.ComboDay.value;
var Now = new Date();
var CurrentDate = Now.getYear() + &quot;-&quot; + (Now.getMonth()+1) + &quot;-&quot; + Now.getDate()
if (ProposedDate < CurrentDate){
alert(&quot;The date you have entered has passed&quot;);
return;
}

For some reason it thinks that the 10th Dec 2002 is before todays date (6th Dec 2002).
Any help would be much appreciated.

 
Its because you are comparing strings.

&quot;2002-12-10&quot; is less than &quot;2002-12-6&quot;

If Now.getDate() padded the date with zeros (e.g. returned 06 instead of 6 on the sixth of the month), then your method would work. But since it doesn't, try this (use the actual date objects instead of their string representations):

var ProposedDate = new Date(Form_NewSolution.ComboMonth.value + &quot;/&quot; + Form_NewSolution.ComboDay.value + &quot;/&quot; + Form_NewSolution.ComboYear.value);
var Now = new Date();
if (ProposedDate < Now){
alert(&quot;The date you have entered has passed&quot;);
return;
}

should work. You might have to put some error catching code around where ProposedDate is constructed (since valid values might not be selected in the combo boxes)

cheyney
 
That works really well but there is one small problem. Because I have no means of the user selecting the time (and I don't want one) it defaults the ProposedDate to a time value of 00:00:00

So if the date I have entered is todays date, it tells me it has passed because the Now value is set to the current time (which is greater than 00:00:00).

Is there any means of coding a default time value of say 23:59:59 ?
 

// get the maximum datetime that is still today
var Now = new Date();
Now.setHours(23);
Now.setMinutes(59);
Now.setSeconds(59);
Now.setMilliseconds(999);

 
Supplying the hours, mins, secs n mill with the max values doesn't help. Instead I tried with the zero values so that the current date will have 0 values as the parameters for the hours, mins, secs n mills values.

it looks like the below given code

var Now = new Date();
Now.setHours(0);
Now.setMinutes(0);
Now.setSeconds(0);
Now.setMilliseconds(0);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top