Creating a calendar using ColdFusion

Readability

Creating a calendar using ColdFusion

In a pre­vi­ous exam­ple, “Get­ting started with Dates in Cold­Fu­sion”, we saw how you could get started with cre­at­ing and for­mat­ting date val­ues, as well as many other date related func­tions in ColdFusion.

The fol­low­ing exam­ples show how you can cre­ate a cou­ple of sim­ple cal­en­dar con­trols in Cold­Fu­sion using some loops and var­i­ous date functions.

The first exam­ple shows a sim­ple cal­en­dar that loops over the days in a month and dis­plays each day in a sep­a­rate table row:






        
        
#dateFormat(firsties, "mmmm yyyy")#
#dateFormat(tempDate, "d (ddd)")# ...

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

Novem­ber 2010
1 (Mon)
2 (Tue)
3 (Wed)
4 (Thu)
5 (Fri)
6 (Sat)
7 (Sun)
8 (Mon)
9 (Tue)
10 (Wed)
11 (Thu)
12 (Fri)
13 (Sat)
14 (Sun)
15 (Mon)
16 (Tue)
17 (Wed)
18 (Thu)
19 (Fri)
20 (Sat)
21 (Sun)
22 (Mon)
23 (Tue)
24 (Wed)
25 (Thu)
26 (Fri)
27 (Sat)
28 (Sun)
29 (Mon)
30 (Tue)

The sec­ond exam­ple shows a more tra­di­tional “box” type cal­en­dar and uses a bit more com­plex date calculations:







        
        
        
        
            
                
        
< #dateFormat(firsties, "mmmm yyyy")# >
#left(dayOfWeekAsString(cDay), 1)#
  #day(thisDay)#
 

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

<
Novem­ber 2010
>
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

In a previous example, “Getting started with Dates in ColdFusion”, we saw how you could get started with creating and formatting date values, as well as many other date related functions in ColdFusion.

The following examples show how you can create a couple of simple calendar controls in ColdFusion using some loops and various date functions.

The first example shows a simple calendar that loops over the days in a month and displays each day in a separate table row:

<cfparam name="URL.dateObj" default="#now()#" type="date" />
<cfset firsties = createDate(year(dateObj), month(dateObj), 1) />
 
<cfoutput>
<table border="1">
<thead>
    <tr>
        <th colspan="2">#dateFormat(firsties, "mmmm yyyy")#</th>
    </tr>
</thead>
<tbody>
    <cfloop from="0" to="#daysInMonth(firsties)-1#" index="cDay">
        <cfset tempDate = dateAdd("d", cDay, firsties) />
        <tr>
          <td title="#dateFormat(tempDate, 'medium')#">#dateFormat(tempDate, "d (ddd)")#</td>
          <td width="200">...</td>
        </tr>
    </cfloop>
</tbody>
</table>
</cfoutput>

The previous example outputs the following text:

November 2010
1 (Mon)
2 (Tue)
3 (Wed)
4 (Thu)
5 (Fri)
6 (Sat)
7 (Sun)
8 (Mon)
9 (Tue)
10 (Wed)
11 (Thu)
12 (Fri)
13 (Sat)
14 (Sun)
15 (Mon)
16 (Tue)
17 (Wed)
18 (Thu)
19 (Fri)
20 (Sat)
21 (Sun)
22 (Mon)
23 (Tue)
24 (Wed)
25 (Thu)
26 (Fri)
27 (Sat)
28 (Sun)
29 (Mon)
30 (Tue)

The second example shows a more traditional “box” type calendar and uses a bit more complex date calculations:

<cfparam name="URL.dateObj" default="#now()#" type="date" />
<cfset firsties = createDate(year(dateObj), month(dateObj), 1) />
<cfset lasties = dateAdd("d", daysInMonth(firsties)-1, firsties) />
 
<cfoutput>
<table border="1">
<thead>
    <tr bgcolor="##CCCCCC">
        <th colspan="7">
            <a href="?dateObj=#dateFormat(dateAdd("m", -1, firsties), "short")#" title="previous month">&lt;</a>
            #dateFormat(firsties, "mmmm yyyy")#
            <a href="?dateObj=#dateFormat(dateAdd("m", 1, firsties), "short")#" title="next month">&gt;</a>
        </th>
    </tr>
    <tr bgcolor="##CCCCCC">
    <cfloop from="1" to="7" index="cDay">
        <th>#left(dayOfWeekAsString(cDay), 1)#</th>
    </cfloop>
    </tr>
</thead>
<tbody>
    <tr>
    <cfif dayOfWeek(firsties) GT 1>
        <td colspan="#dayOfWeek(firsties)-1#" bgcolor="##EEEEEE">&nbsp;</td>
    </cfif>
    <cfloop from="0" to="#daysInMonth(firsties)-1#" index="cDay">
        <cfset thisDay = dateAdd("d", cDay, firsties) />
        <td>#day(thisDay)#</td>
        <cfif dayOfWeek(thisDay) EQ 7>
            </tr>
            <cfif cDay NEQ (daysInMonth(firsties)-1)>
                <tr>
            </cfif>
        </cfif>
    </cfloop>
    <cfif (7-dayOfWeek(lasties)) GT 0>
        <td colspan="#7-dayOfWeek(lasties)#" bgcolor="##EEEEEE">&nbsp;</td>
    </cfif>
    </tr>
</tbody>
</table>
</cfoutput>

The previous example outputs the following text:

<
November 2010
>
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30  

Leave a Reply

Your email address will not be published.