Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(ColdFusion) Compare two dates, use result to determine what displays

Tags:

coldfusion

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?

like image 637
Sirius Mane Avatar asked Jun 29 '12 19:06

Sirius Mane


1 Answers

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. :)

like image 111
Busches Avatar answered Sep 29 '22 20:09

Busches