http://www.chrispearson.org/pages/programming/vbscript/datework/misc/ASPcalendar.asp
09h32
Wednesday, 8. October 2008

CALENDAR

The creation of a calendar must use the WeekDay function if the calendar doesn't use the default week format - That is, if the calendar runs Monday through to Friday, followed by the weekend, then the usual system default week of Sunday through to Saturday won't be compatible. As these examples using today's date show:

WeekDay() with week beginning on the default day: 4 ,
from the code
   <% =WeekDay(Now) %>

WeekDay() with week beginning Monday: 3 , coded
   <% = WeekDay(Now, vbMonday) %>

First day of this month ( Wednesday ) goes in column 3 when rows start with Monday, as in

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

from the VBScript
   Weekday(DateSerial(Year(Now), Month(Now), 1), vbMonday)

So, to create a calendar table for this month, we need to create

a header row with the day names, beginning on Monday
and ending with Sunday

this we can do in HTML, as above

four to six rows of seven cells per row

empty cells before the first day of the month

rows of the month's days, each containing seven cells

empty cells following the last day of the month
to place seven cells in the final row

 

Overview

This example uses date functions to create a dynamic calendar, outputting a HTML table.

The calendar can be configured to run from Monday to Sunday or any other sequence of days, irrespective of the way the server is configured (Usually with weeks starting on Sunday but probably varying with different international options)

See also the notes on date functions and on days of the week

Return to the main article

Step 1: The header row

Start the table and write out the header row in HTML with ASP title

   Calendar of <% =MonthName(Month( 08/10/2008 09:32:26 )) %>, <% =Year( 08/10/2008 09:32:26 ) %><BR>
   <table width="100%" border="1" cellpadding="4" cellspacing="0" bordercolor="#009933">
   <tr>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
     
Monday</font></td>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
      Tuesday</font></td>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
       Wednesday</font></td>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
      Thursday</font></td>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
      Friday</font></td>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
      Saturday</font></td>
   <td width="10%"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
      Sunday</font></td>
   </tr>

Step 2: Loop through empty, pre-first day cells then days of the month

Number of empty cells before first of month: intPreFirst

intPreFirst = WeekDay(DateSerial( 2008 , 10 , 1), vbMonday) - 1

So the VBScript Response.Write intPreFirst would output 2

The last day of the calendar month goes in the column calculated from
Empty cells before first of month + Days in the month
So the number of empty cells required to fill the last row is
7 (the number of columns) less the remainder of used cells divided by 7, as shown in the code fragments below

intPreFirst = WeekDay(DateSerial( 2008 , 10 , 1), vbMonday) - 1  ' Value is 2
intDayCount = Day(DateAdd("d", -1, DateSerial( 2008 , 10 + 1, 1))) ' Value is 31
intPostLast = 7 - (intPreFirst + intDayCount mod 7) ' Value is 2 - Month ends on a Friday

The calendar code
  Top of this page 

<%
   Dim intPreFirst
   Dim intDayCount
   Dim PostLast
   Dim i ' Loop counter
'
   intPreFirst = WeekDay(DateSerial( 2002 , 8 , 1), vbMonday) - 1
   intDayCount = Day(DateAdd("d", -1, DateSerial( 2002 , 8 + 1, 1)))
   intPostLast = 7 - (intPreFirst + intDayCount mod 7)
'
' Start first row
'
   Response.Write "<TR>"
'
' Cells before first of month
'
   For i = 1 to intPreFirst
   Response.Write "<TD>&nbsp;</TD>" & vbCR
   Next
'
' The days of the month
'
   For i = intPreFirst + 1 to intPreFirst + intDayCount
      If i mod 7 = 1 Then ' Start of a new row?
         Response.Write "<TR>"
      End If
      Response.Write "<TD align='right' valign='top'>" & _
         "<font color='#000000' size='1' face='Verdana, Arial, Helvetica, sans-serif'>" & _
         i - intPreFirst & _
         "<BR>&nbsp;</font></TD>" & _
         vbCR
      If i mod 7 = 0 Then ' End of a row?
         Response.Write "</TR>" & vbCR
      End If
   Next
'
' Wrap up the last row
'
   For i = 1 To intPostLast
      Response.Write "<TD>&nbsp;</TD>" & vbCR
   Next
'
' End the table in HTML
'
%>

Giving the output shown here:

Calendar of October , 2008

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    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
 
31
 
   

Top of this page

Return to the main article

     

xxx,xxx

copyright ©2000 - 2008 Chris Pearson