A few days ago, I demoed a small page that output a calendar month in ColdFusion by looping over the dates in a rather slick index loop. This calendar, as all calendars that I have built, start with Sunday as the first day of the week. Apparently, this is not how the entire world does it, and in response to a question posed by Macbuoy, I created a ColdFusion user defined function called MyDayOfWeek() which wraps around ColdFusion's built-in DayOfWeek() method. This UDF translates the traditional day of week calculation into the day of week of your choice.
There were some issues for people trying to applying this ColdFusion UDF to the calendar month display, so I thought I would just demo how it works. The steps to do this were quite easy:
Then, as before, I have to start off by building my SQL query to test with:
Launch code in new window » Download code as text file »
And, here is the modified code. For demo purposes, I am including the ColdFusion user defined function, MyDayOfWeek(), in the file itself, but this could be included with your standard UDFs:
Launch code in new window » Download code as text file »
Running the above, we get the updated calendar month output with Monday leading off the week:
| | | | ||
| | ![]() | | ||
| | | |
Now, I should really figure out how to get the headers to be output based on the day of the week method :) Then we would be all set.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Print Page
Lenny And Bo, ColdFusion Programmers (Vol. 12)
Sending Text Messages (SMS) With ColdFusion And CFMail
Works like a charm.
For the header row, I'm gonna looped over the list (2,3,4,5,6,7,1)--along with DayOfWeekAsString without a similar UDF shift.
Posted by macbuoy on May 9, 2007 at 4:27 PM
Sweeet. Sounds good to me, to handle the headers that way.
Posted by Ben Nadel on May 9, 2007 at 4:29 PM
that's exactly how i've done it in the past. You could probably use some combination of list functions to get the order of that list starting with your first day of week variable. something like:
list = 1,2,3,4,5,6,7;
firstdayofweek = 2;
for i = 1; i < firstdayofweek; i++
{
shift = listgetat(list, i);
list = listrest(list);
list = listappend(list, shift);
}
Posted by duncan on May 10, 2007 at 4:39 AM
that wouldn't work obviously, as the first line would always be out of sync as we're updating the list in the loop. if we made a copy of the list and updated that instead...
Posted by duncan on May 10, 2007 at 4:41 AM