Skip to main content
Known Participant
August 4, 2010
Answered

cfif condition for dates

  • August 4, 2010
  • 1 reply
  • 4824 views

hi i need to check date for 3 days and if that is date i have to display link or else have to display another

ex:

<cfoutput>

<cfset todaydate="#DateFormat(now(),"mm/dd/yyyy")#"/>

<cfif  #todaydate# is "08/04/2010" or #todaydate# is "08/05/2010>

this is to display

<cfelse>

this has to display

</cfoutput>

is the above conditional statement is correct or not? please help me

This topic has been closed for replies.
Correct answer Steve Sommers

That will work for this specific example, but you are treating the dates as strings and if you continue programming in CF, this way will be bulky and confusing.

I would do the following instead:

<cfset todaydate = now() />
<cfif todaydate GTE CreateDate(2010,8,4) and todaydate LT CreateDate(2010,8,7)>
            <a href="test.cfm" hidefocus="true">Test</a>
<cfelse>
            <a href="test2.cfm" hidefocus="true">test2</a>
</cfif>

Using this method, if you decide to do this again for a 90 day window, the if statement stays about as big as it is -- using your method will require a lot of dates to be typed.

1 reply

Legend
August 4, 2010

<cfoutput>

<cfset todaydate=Now() />

<cfset begdate=DateAdd("d",-2,todaydate) />

<cfif  somedate GTE begdate>

     <!-- date is within 3 days - today, yesterday, or day before -->

<cfelse>

     <!-- date is olderthan 3 days -->

</cfif>

</cfoutput>

cfnewAuthor
Known Participant
August 4, 2010

i need for today , tommorow and dayafter tomorrow. below code is correct or not?

<cfset todaydate = "#DateFormat(now(),"mm/dd/yyyy")#"/>
            <cfif (#todaydate# is "08/04/2010") OR (#todaydate# is "08/05/2010") OR (#todaydate# is "08/06/2010")>
    
            <a href="test.cfm" hidefocus="true">Test</a>
            <cfelse>
            <a href="test2.cfm" hidefocus="true">test2</a>
            </cfif>

because i just need to display link only for next 3 days

Steve SommersCorrect answer
Legend
August 4, 2010

That will work for this specific example, but you are treating the dates as strings and if you continue programming in CF, this way will be bulky and confusing.

I would do the following instead:

<cfset todaydate = now() />
<cfif todaydate GTE CreateDate(2010,8,4) and todaydate LT CreateDate(2010,8,7)>
            <a href="test.cfm" hidefocus="true">Test</a>
<cfelse>
            <a href="test2.cfm" hidefocus="true">test2</a>
</cfif>

Using this method, if you decide to do this again for a 90 day window, the if statement stays about as big as it is -- using your method will require a lot of dates to be typed.