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!

datecompare

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
0
0
US
I have three dates, and need to check the dates for these criterias:
1. End_Date cannot be earlier then or equal to Start_Date
2. End_Date cannot be later then or equal to Expire_Date
3. Expire_Date cannot be earlier then or equal to Start_Date
4. Expire_Date cannot be earlier then or equal to End_Date

This is how I am doing it, but seems like a lot of work. Is there a better (and quicker) way?

Thanks.

Code:
<cfif isdate(FORM.Start_Date) and isdate(FORM.End_Date)>
	<cfset comparison = datecompare(FORM.End_Date, FORM.Start_Date)>

	<cfswitch expression="#comparison#">
		<cfcase value="-1">
			<!--- 'FORM.End_Date' IS EARLIER THEN OR EQUAL TO 'FORM.Start_Date' --->
			<cflocation url="" addtoken="no">					
		</cfcase>
		
		<cfcase value="0">
			<!--- 'FORM.End_Date' IS EARLIER THEN OR EQUAL TO 'FORM.Start_Date' --->
			<cflocation url="" addtoken="no">					
		</cfcase>			
	</cfswitch>
</cfif>

<cfif isdefined("FORM.Expire_Date")>
	<cfif (isdate(FORM.Start_Date) and isdate(FORM.Expire_Date)) or (isdate(FORM.End_Date) and isdate(FORM.Expire_Date))>
		<cfset comparison = datecompare(FORM.Expire_Date, FORM.Start_Date)>
		<cfset comparison2 = datecompare(FORM.Expire_Date, FORM.End_Date)>

		<cfswitch expression="#comparison#">
			<cfcase value="-1">
				<!--- 'FORM.Expire_Date' IS EARLIER THEN OR EQUAL TO 'FORM.Start_Date' --->
				<cflocation url="" addtoken="no">					
			</cfcase>
			
			<cfcase value="0">
				<!--- 'FORM.Expire_Date' IS EARLIER THEN OR EQUAL TO 'FORM.Start_Date' --->
				<cflocation url="" addtoken="no">					
			</cfcase>			
		</cfswitch>
		
		<cfswitch expression="#comparison2#">
			<cfcase value="-1">
				<!--- 'FORM.Expire_Date' IS EARLIER THEN OR EQUAL TO 'FORM.End_Date' --->
				<cflocation url="" addtoken="no">					
			</cfcase>
			
			<cfcase value="0">
				<!--- 'FORM.Expire_Date' IS EARLIER THEN OR EQUAL TO 'FORM.End_Date' --->
				<cflocation url="" addtoken="no">					
			</cfcase>			
		</cfswitch>
	</cfif>
</cfif>

[sub]
____________________________________
Just Imagine.
[sub]
 
<CFIF IsDate(form.Start_Date)
AND IsDate(form.End_Date)
AND End_Date GT Start_Date
AND End_Date LT Expire_Date
AND Expire_Date GT Start_Date
AND Expire_Date GT End_Date>
<!--- validation passed --->
<CFELSE
<!--- validation failed --->
</CFIF>

r937.com | rudy.ca
 
r937, the Expire_Date is optional. I tried that first and it always failed cause the clause was AND (meaning all criteria had to pass).



[sub]
____________________________________
Just Imagine.
[sub]
 
<validate all dates first...>

<cfif form.endDate GT form.startDate>

<cfif structKeyExists(form,"expireDate")>
<cfif isDate(form.expireDate)
AND form.expireData GT endDate>

Passed with expire Date
<cfelse>
Expire date failed.
</cfif>

Passed without expire date

<cfelse>

Failed

</cfif>

only check if expire_date is greater than end_date. If it is, than it is gt start_date too...

2. End_Date cannot be later then or equal to Expire_Date
3. Expire_Date cannot be earlier then or equal to Start_Date
4. Expire_Date cannot be earlier then or equal to End_Date

could be written as expire_date must be GT end_date

all conditions are met with that statement. #2 and #4 are identical statements. And #4 covers #3.

DISCLAIMER: I have been working accounting all day, so my logic could be so wrong right now...

 
actually, I would cfparam all dates with a default value of "" so that the variable exists, but is not a date. and then check isDate while checking your other requirements for pass or fail.

<cfparam name="form.startDate" default="">
<cfparam name="form.endDate" defualt="">
<cfparam name="form.expireDate" default="">

<cfif isDate(form.startDate)
AND isDate(form.endDate)
AND form.endDate GT form.startDate>

<cfif isDate(form.expireDate)
AND form.expireData GT endDate>

Passed with expire Date

<cfelse>

Expire date failed.

</cfif>

Passed without expire date

<cfelse>

Failed

</cfif>

that way, as soon as form. expireDate is a valid date, that means it was sent with the form, so use it.

 
imstillatwork, sounds like a good idea. I will test this on Tuesday (Monday is a holiday). I will just use cfparam to declare the vars, which is better then doing the "isdefined" route.

[sub]
____________________________________
Just Imagine.
[sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top