Skip to main content
December 1, 2013
Answered

How can I determine if the current date is between two dates?

  • December 1, 2013
  • 1 reply
  • 1125 views

I am trying to display a link when the current date falls between two dates. I have setup the startdate and the enddate.When the current date falls between the startdate an the enddate, the link will apear. Both startdate and enddate have already been formatted earlier as mm/dd/yyyyy. I tried using the following script, to no avail. What am I doing wrong?

<cfif startDate GTE #dateformat(now(), 'MM/DD/YYYY')# AND enddate LTE #dateformat(now(), 'MM/DD/YYYY')#>

    This topic has been closed for replies.
    Correct answer

    Not sure what you are talking about. I have been using the #dateformat(date_entered, 'mm/dd/yyyy')# for years with no problem. I googled the datediff function before posting here and what I found is datediff  determines the integer number of units by which date1 is less than date2. I don't quite know how this can help me.

    1 reply

    Inspiring
    December 1, 2013

    Well, I think if you get a pen and paper and try a couple of dates, you'll see the basic math problem with storing the dates as mm/dd/yyy.  You have two choices.  One is to represent the dates as YYYYMMDD, so that two dates can be compared.  The other is to store the dates as date datatypes, and use the dateDiff() function to do the compares.  If you look up or Google the CF dateDiff function you'll see how to use it and how to create date datatypes.

    -reed

    Correct answer
    December 1, 2013

    Not sure what you are talking about. I have been using the #dateformat(date_entered, 'mm/dd/yyyy')# for years with no problem. I googled the datediff function before posting here and what I found is datediff  determines the integer number of units by which date1 is less than date2. I don't quite know how this can help me.

    Participating Frequently
    December 2, 2013

    DateFormat() returns a string, not a date object.

    Suppose you have two strings like so:

    one = "aaab";

    two = "abaa";

    and you compare them:

    <cfoutput>#one GT two#</cfoutput>

    you'll get NO (or FALSE), because two is 'greater' than one.

    Now suppose you have two strings like so:

    date1 = "10/31/2013";

    date2 = "12/31/2012";

    Now you and I can see that date1 refers to a later date than date2.  But if you use CF to say if date1 GT date2, it should treat them as strings.  And doing it that way, the string starting "12" is greater than the string starting "10". 

    So Reed's answer is correct. Although in testing I found that CF must be recognising the strings as looking like dates and actually doing a date comparison, which sounds more like a bug than a feature to me, and a dangerous thing to assume.  I'd much rather construct two date objects and use dateDiff()