Reputation: 213
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?
Upvotes: 11
Views: 12120
Reputation: 4141
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)
Upvotes: 0
Reputation: 1964
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. :)
Upvotes: 18
Reputation: 2073
<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>
Upvotes: 1