Skip to main content
Inspiring
May 5, 2008
Question

Time difference?

  • May 5, 2008
  • 2 replies
  • 294 views
I know you can compare dates using the datediff function, but is there
anything that can be used to compare times?
    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    May 5, 2008
    It is not a science but, even with datediff, you can achieve simple results like

    <cfset t1="9:20 am">
    <cfset t2="11:07 pm">
    diffence between 9:20 am and 11:07 pm is <cfoutput>#datediff('n',t1,t2)#</cfoutput> minutes.

    Inspiring
    May 5, 2008
    Hi, Steve,

    You can use the CFML function DateCompare to compare two dates, including times. The optional third parameter allows you to specify the precision to use (compare dates down to the day, hour, minute, second, etc.

    <cfscript>
    d1 = "05/04/2008 17:45:00";
    d2 = "05/04/2008 17:44:00";
    myResult = DateCompare(d1,d2,"n");
    //the above line will compare the dates down to the minute (the "n") and
    // return 0 for equal dates, -1 if d1 is earlier than d2, or 1 if d2 is greater than d1
    </cfscript>

    The key is to pass then entire date (date and time) to the function.

    Hope that helps.

    Cheers,
    Craig