This Blog is Moving to http://blog.drisgill.com
Viewing By Entry / Main
July 17, 2006
Had a need for a simple calendar application base in ColdFusion, and I didn't really want to write my own. I was looking at Ray's post on the topic and wasn't too keen on any of the solutions provided (either cost money or had too many unnecessary features for my app). I did find just what I was looking for on cflib.org though, and modified it slightly to include some previous and next buttons... and viola... I give you the Simple ColdFusion Calendar Function (Example | Source). Feel free to use it however. Thanks to William Steiner for 95% of the code.

Comments

SWEET!

Really glad to see my little function help someone out.

Hey, thanks for this code it's working great for an app I am writing. One thing: the year doesn't change between Dec / Jan. I added this hack and it changes the year:

<blockquote>
outString = outString & '>#DateFormat(firstDay, "mmmm yyyy")#</td><td class="header"><a class="navmonth" href="roominfo.cfm?room=#roomNum#&amp;type=calendar&amp;thisMonth=#month(dateadd("M",1,firstDay))#&amp;';

   if( month(dateadd("M",1,firstDay)) EQ 1 )
      outString = outString & 'thisYear=#year(dateadd("Y",1,CreateDate(curYear + 1, curMonth, 1)))#">Next</a></td></tr>';
   else   
      outString = outString & 'thisYear=#year(dateadd("Y",1,firstDay))#">Next</a></td></tr>';

   outString = outString & '<tr><td class="subheader">Sun</td><td class="subheader">Mon</td><td class="subheader">Tue</td><td class="subheader">Wed</td><td class="subheader">Thu</td><td class="subheader">Fri</td><td class="subheader">Sat</td></tr>';
</blockquote>

hey Chief, your code seems to have an error or maybe i'm just not getting it. i couldn't make your little hack work. what's up?

Kevin, not sure why it's not wroking, you don't include the <blockquotes> of course, I was just trying to display it nicely on the blog here, but I guess he doesn't allow HTML code. Feel free to e-mail me t e h s u c k [at] gmail and I can possibly help you out.

Hmmm.... I'm a newbie here and I can't figure out how to get this "hack" to work. I also noticed that your Example on the site works, but your Source code contains the Year bug. Can you help please?

To fix the year forwarding, simply add the following just before the calendar output:

<cfif url.thismonth eq 1><cfset url.thisyear = url.thisyear + 1></cfif>

Sorry guys... let me know if this fix doesn't do the trick. I'll look into updating the code.

Chief's code in theory works however it looks like he modified it for his own application. The code will not work to just copy and paste into your own application. This is his hack which can be used to directly replace the similar code block in the above calendar script.

   outString = "<table border='0' cellpadding='5' cellspacing='3'><tr><td class='header'>";
   outString = outString & "<a class='navmonth' href='cal.cfm?thisMonth=#month(dateadd('M',-1,firstDay))#&thisYear=#year(dateadd('Y',-1,firstDay))#'>Prev</a>";
   outString = outString & "</td><td class='header' align=center colspan='5'>#DateFormat(firstDay, 'mmmm yyyy')#</td><td class='header'>";
   
if( month(dateadd("M",1,firstDay)) EQ 1 )
   outString = outString & "<a class='navmonth' href='cal.cfm?thisMonth=#month(dateadd('M',1,firstDay))#&thisYear=#year(dateadd('Y',1,CreateDate(curYear + 1, curMonth, 1)))#'>Next</a>";
else
   outString = outString & "<a class='navmonth' href='cal.cfm?thisMonth=#month(dateadd('M',1,firstDay))#&thisYear=#year(dateadd('Y',1,firstDay))#'>Next</a>";

outString = outString & "</td></tr>";
   outString = outString & "<tr><td class='subheader'>Sun</td><td class='subheader'>Mon</td><td class='subheader'>Tue</td><td class='subheader'>Wed</td><td class='subheader'>Thu</td><td class='subheader'>Fri</td><td class='subheader'>Sat</td></tr>";

Hi all,
I have a much simpler fix for the year issue - based on the code available today (16 October 2007).

On lines 31 and 32, where it uses DateAdd() to calculate the "thisYear" URL parameter, simply change it from adding/subtracting a year to adding/subtracting a month (change the first parameter from "Y" to "M").

This works for both going forward from December to January the next year and back from January to December the previous year.

Regards,
Paul

Hey thanks for the simple calendar. It works great.

Not to sound ungreatful, but i want that calendar -----> (the one you have on the top right of the screen, that is clickable with events)

Awesome. This is a nice simple calendar I can link to a database and have fun with. Thanks for the starter.

And thanks to Paul for the 'M' fix.

I want to make the current date a link to a master calendar.

I thought this was the place, but had no luck.

// insert a day
      outString = outString & "<td align='left' valign='top' width='#width#px' ";
      if(CreateDate(curYear, curMonth, i) eq CreateDate(year(now()),month(now()),day(now()))){
         outString = outString & "class='day-current'"& "<a href=''></a>";
      }else{
         outString = outString & "style='background-color: whitesmoke' ";
      }
      outString = outString & "height='#height#'>#i#<br></td> ";