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.

jgroove

Programmer
Jul 9, 2001
43
0
0
US
I am trying to test whether the enddate is less than the startdate. I am using the date compare function and it is not returning what I expect. Here is the code that I am using:

Code:
<cfset StartDate=CreateDate (form.selStartDateYear,form.selStartDateMonth,form.selStartDateDay)>
<cfset EndDate=CreateDate (form.selEndDateYear,form.selEndDateMonth,form.selEndDateDay)>

<cfoutput>StartDate=#StartDate#<br>EndDate=#EndDate#<br>DateCompare=#DateCompare(EndDate,StartDate)#</cfoutput>

Here is the output from this code:

StartDate={ts '2002-01-01 00:00:00'}
EndDate={ts '2003-07-05 00:00:00'}
DateCompare=1

from the documentation
DateCompare performs a full date/time comparison of two dates. Returns:

-1 if date1 is less than date2
0 if date1 is equal to date2
1 if date1 is greater than date2

to me it looks as if StartDate < EndDate and that is should return -1

Can someone shed some light on this??
 
May I suggest you reverse the order in your DateCompare like this? ;-)

<cfoutput>StartDate=#StartDate#<br>EndDate=#EndDate#<br>DateCompare=#DateCompare(StartDate,EndDate)#</cfoutput>
Calista :-X
Jedi Knight,
Champion of the Force
 
*bangs head on desk*


Thank you for pointing that out. The problem is that I had a <cfif DateCompare(StartDate,EndDate)> later in the page and when I changed them, I didn't realize they were opposites and kept changing them back and forth. I knew something was amiss. D'oh!!!

it has been one of those days,
jgroove
 
Been there. Done that. We all need an extra pair of eyeballs once in awhile. Calista :-X
Jedi Knight,
Champion of the Force
 
Don't have a reply but a question about date functions. Can you tell me how to just get the month or day or year alone. Iwant to input each into a separate field in Db?

Thanks,

Norm
 
NHRockwell,

There are many different ways of accomplishing this task. The easist might be a function called DatePart that takes two arguments. The first is the datepart you are looking for and the second is the date.

It looks like this DatePart(datepart,date)

possible dateparts are
Year=yyyy
quarter=q
Month=m
Day of year=y
day=y
weekday=w
week=ww
hour=h
minute=n
second=s

Possible uses : <cfset todaysyear=DatePart(&quot;yyyy&quot;,Now())>


Depdending on your db, there are db functions that do this as well.


hope this helps out,
jgroove
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top