11

I'm working on a client's site, and he needs coupon expiration dates to change red if they're two weeks or less to expiring. Otherwise they will be black.

The site itself was not made by me. I just started here and they want me to learn ColdFusion. So I've been stumbling through it.

I thought that maybe by using DateCompare or DateDiff I could get what I want. However I get garbled text when I <CFOUTPUT> the results of either comparison. It displays a long string of 51515151551 that gradually gets smaller and smaller for each subsequent coupon item on the customers list.

The coupon date itself (ie end_date) is called from the Microsoft SQL database. It is formatted using Dateformat: #dateformat(end_date,"m/d/yyyy")#.

I tried to compare it with Now() in order to dynamically determine whether or not the expiration date should be colored red. I've also tried formatting Now(), I get the same results.

Any seasoned programmers here that could lead me to the right path?

3 Answers 3

18

Datediff is what you want. Are you using the correct date part in date diff? You could use 'ww' for weeks or 'd' for days, I used days in the below example.

<cfset CouponDate = createDate( 2012, 05, 29 ) />
<cfif DateDiff( "d", CouponDate, Now() ) GTE 14>
    <cfset Expired = False />
<cfelse>
    <cfset Expired = True />
</cfif>

Obviously you don't need to set a variable or anything, this is just some example code to get your idea working. :)

6
  • Yeah, what he said, verbatim. :)
    – Evik James
    Commented Jun 29, 2012 at 19:47
  • 3
    I think the expired values are reversed.
    – Leigh
    Commented Jun 29, 2012 at 19:52
  • They are! Fixing... I switched it up so CouponDate was first then Now() and didn't swap them around too. Good catch.
    – Busches
    Commented Jun 29, 2012 at 19:54
  • 1
    Hrm, I'm looking into this solution right now. This is great! It's good to know that I wasn't far off from the answer. I love stackoverflow. Commented Jun 29, 2012 at 19:58
  • Be careful because datediff in Microsoft SQL Server and Adobe ColdFusion have a lot of similarities. Commented Jun 30, 2012 at 16:28
1
<cfscript>
// setup first test date for 7 days ago
date1 = dateAdd( 'd', -7, now() );
// setup second date for 14 days ago
date2 = dateAdd( 'd', -14, now() );
// compare date 1
writeOutput( dateDiff('d', date1, now() ) & '<hr/>');
// compare date 2
writeOutput( dateDiff('d', date2, now() )  & '<hr/>');
// demonstrate use
if ( dateDiff('d', date2, now()) gte 14 ) {
    writeOutput( 'RED' );
}
</cfscript>
0

This worked for me:

<cfset dtdiff = query.expiry_date - Now() />
<cfif Round(dtdiff) LEQ 14>
    Red
<cfelse>
    Black
</cfif>

if you wan't to use 14 days with time difference you can use

Fix(dtdiff) instead of Round(dtdiff)

3
  • i had issue with comparing mssql timestamp to coldfusion Now(). with this i fixed the issue. and i am sure Stackoverflow expects diff answers.. and this may surely help others. Commented Jan 27, 2017 at 5:01
  • What kind of issue? (If you really mean timestamp that is totally different than date/datetime.) When it comes to dates, best not to roll-your-own logic unless there is a very good reason.
    – Leigh
    Commented Jan 27, 2017 at 14:36
  • issue with diff between mssql timestamp and now. anyways different answers are encouraged in stackoverflow. Commented Jan 31, 2017 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.