4 Replies - 4321 Views - Last Post: 16 September 2010 - 10:16 AM Rate Topic: -----

#1 jli112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 08

Compare date (month only) to determine display text

Posted 15 September 2010 - 06:24 AM

I am trying to pull the current month information from the current date and use that information to display certain text. In plain terms I want to be able to determine if the current month is january or after, and if it is, display the word fall. Or determine if the current month is may or after, and if it is, display the word spring.

This bit of code seems to be working, the word fall is displaying. But is it properly formatted that come january the word spring will display?

<cfif Now() GTE 1>
	Fall

<cfelseif Now() GTE 5>
	Spring

</cfif>



Any suggestions would be greatly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Compare date (month only) to determine display text

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,441
  • Joined: 13-January 08

Re: Compare date (month only) to determine display text

Posted 15 September 2010 - 06:44 AM

Much easier and probably better way to determine the month from a date. CF has a built in function called Month(). You pass in the date and it passes back a value from 1 to 12 telling you the month for that date.

That said, your explained logic is a bit flawed. If January or later = fall. If May or after = spring. Given that, what would you display for say October? October meets both requirements (after January and after May). In a CFIF/CFELSEIF sift, the first condition that is met is what comes out. Therefore, your code that says if the "month is equal to or greater than January"...well, that one will always be true so your logic answer will never change.

To make your stated logic work your code would need to look like this:
<cfif Month(Now()) GTE 1 AND Month(Now()) LTE 4>
   Fall
<cfelseif Month(Now()) GTE 5>
   Spring
</cfif>


That will produce the following: for January-April = Fall; May-December = Spring.

If you haven't yet seen the Coldfusion Resources thread that's pinned at the top of the CF forum, go check it out. Lots of handy stuff there for you.

Good luck!
Was This Post Helpful? 1
  • +
  • -

#3 jli112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 08

Re: Compare date (month only) to determine display text

Posted 15 September 2010 - 07:03 AM

I knew something was off about the logic, and therefore something missing. I applied your suggestion, I switched the display text, I believe I explained it wrong. January through April should read Spring, may through December should read fall (graduation dates). Once I read through the code to understand what was going on, it made complete sense as to what I was doing wrong. Its working perfectly now. :^:

Thanks so much for your reply, and for the sources tip, I'll be looking through that today. :)
Was This Post Helpful? 0
  • +
  • -

#4 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,441
  • Joined: 13-January 08

Re: Compare date (month only) to determine display text

Posted 15 September 2010 - 07:06 AM

Glad to help.

If you run across a CF resource please feel free to add to that CF resources thread as well.
Was This Post Helpful? 0
  • +
  • -

#5 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Compare date (month only) to determine display text

Posted 16 September 2010 - 10:16 AM

@jli112 from what I quickly read you are just trying to make a function to pass a date into and it returns the season. A quick look on CFLib.org (a library of custom UDF's (user defined functions) comes up with this function http://cflib.org/ind...byid&udfid=894. You may want to check it out if only to see how the developer did it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1