Nov 12, 2009

Little known but helpfull ColdFusion function daysInMonth

Need to find out how many days left in the month?

Here the solution.



Here it is in action.

5 comments:

  1. Worth pointing out that this function takes a -full date- as an argument.

    You'd think (maybe) that you could just pass a month value, but February screws that up every 4 years... much like the American voting population electing a new president (but I digress...)

    Might seem obvious, but I've seen a lot of posts on mailing lists and forums where people get unexpected results due to passing in only a month value :)

    ReplyDelete
  2. Really you'd think they would have taken care of the leap year given that there is a isLeapYear function. I'll change the date on my server see what I come up with.

    http://cfquickdocs.com/#IsLeapYear

    ReplyDelete
  3. Well I get 29 days when I do get daysInMonth() for a leap year and 28 when its not so perhaps they fixed the issue in CF8 and CF9.

    ReplyDelete
  4. No no no... your initial example was/is spot on. You're passing now(), which is a full date value.

    I was just trying to prepare people who might come away from the entry and try something like daysInMonth(n), where n is a value between 1 and 12.

    Can't just pass a month value is all I was trying to say :)

    ReplyDelete
  5. Ahh OK. Thanks for clearing that up Charlie. Now that I re-read your comment I see what you meant.

    ReplyDelete