How to Create a Perpetual Calendar Using Active Server Pages

ID: Q245566


The information in this article applies to:
  • Microsoft Internet Information Services version 5.0


SUMMARY

This article demonstrates how to create a Web page calendar that will not easily go out of date using Microsoft Active Server Pages (ASP).


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
Save the following code to your Web site as "Calendar.asp" in a folder that has at least "script" access defined. When the page is browsed, it will display a calendar with menu buttons that will allow a Web client to choose to display the next or previous month's calendar.

<% @Language="vbscript" %>
<%
  
  ' force declaration of all variables
  Option Explicit
  
  Dim intYear, intMonth, intDay
  Dim intNumberOfDays, strDaysInMonths
  Dim strDate, strMonthName
  Dim strStyle, strTitle
  Dim strMethod, strURL  
  Dim strFirstDay, strLastDay, strCurrentDay
  Dim dtmPrev, dtmNext
  Dim X,Y,Z
  
  ' find out the URL of the page and the HTTP method used
  strMethod = UCase(Request.ServerVariables("REQUEST_METHOD"))
  strURL = LCase(Request.ServerVariables("URL"))
  
  ' if it's a GET then it's a regular browser request
  If strMethod="GET" Then
  
    ' use today's date
    intDay   = CInt(Day(Date()))
    intMonth = CInt(Month(Date()))
    intYear  = CInt(Year(Date()))
    
    strFirstDay = CStr(intMonth) & "-1-" & CStr(intYear)
    strDate  = CStr(intMonth) & "-" & CStr(intDay) & "-" & CStr(intYear)
    
  ' if it's a POST then it's a specific request
  Else
    
    ' get the submitted date
    intDay   = CInt(Day(Date()))
    intMonth = CInt(Request.Form("txtMonth"))
    intYear  = CInt(Request.Form("txtYear"))
    
    strFirstDay = CStr(intMonth) & "-1-" & CStr(intYear)
    strDate     = strFirstDay
    
  End If
  
  ' is a date in a leap year?
  Function IsLeapYearDate(dtmTestDate)
    IsLeapYearDate = False
    If IsDate(dtmTestDate) Then
      Dim dtmTempDate
      dtmTempDate = "1/31/" & Year(dtmTestDate)
      dtmTempDate = DateAdd("m", 1, dtmTempDate)
      If Day(dtmTempDate) = 29 Then IsLeapYearDate = True
    End If
  End Function

  ' create string of days in months
  If IsLeapYearDate("1/1/" & intYear) Then      
    strDaysInMonths = "312931303130313130313031"
  Else
    strDaysInMonths = "312831303130313130313031"
  End If
  
  ' get some date stuff
  strMonthName    = CStr(MonthName(intMonth))
  intNumberOfDays = CInt(Mid(strDaysInMonths,((intMonth-1)*2)+1,2))
  strLastDay      = CStr(intMonth) & "-" & intNumberOfDays & "-" & CStr(intYear)
  
  ' build the page title
  strTitle = "Calendar for " & strMonthName & " " & intYear

  ' get the next and previous months for the menus
  dtmPrev = CDate(DateAdd("m", -1, strDate))
  dtmNext = CDate(DateAdd("m",  1, strDate))
  
%>
<html>
<head>
<title><%=strTitle%></title>
</head>

<body>

<h1 align="center"><%=strTitle%></h1>

<center>
  <table border="0" width="100%">
    <tr>
      <td align="left">
        <form action="<%=strURL%>" method="POST">
          <input type="hidden" name="txtMonth" value="<%=Month(dtmPrev)%>">
          <input type="hidden" name="txtYear" value="<%=Year(dtmPrev)%>">
          <input type="submit" style="width:150" value="View <%=MonthName(Month(dtmPrev))%>">
        </form>
      </td>
    <% If (intMonth <> Month(Date())) Or (intYear <> Year(Date())) Then %>
      <td align="center">
        <form action="<%=strURL%>" method="POST">
          <input type="hidden" name="txtMonth" value="<%=Month(Date())%>">
          <input type="hidden" name="txtYear" value="<%=Year(Date())%>">
          <input type="submit" style="width:150" value="View Current Month">
        </form>
      </td>
    <% End If %>
      <td align="right">
        <form action="<%=strURL%>" method="POST">
          <input type="hidden" name="txtMonth" value="<%=Month(dtmNext)%>">
          <input type="hidden" name="txtYear" value="<%=Year(dtmNext)%>">
          <input type="submit" style="width:150" value="View <%=MonthName(Month(dtmNext))%>">
        </form>
      </td>
    </tr>
  </table>
</center>

<hr>

<table border="1" cellpadding="2" cellspacing="2" width="100%">
<tr>
<%
  ' print the weekday names
  For X = 1 to 7
    Response.Write "<th width=""14%"">" & WeekdayName(X) & "</th>" & vbCrLf
  Next
%>
</tr>
<tr>
<%  
  ' print empty table cells for the beginning days not in the current month
  If (Weekday(strFirstDay)-1) Then  
    For X = 1 to (Weekday(strFirstDay)-1)
      Response.Write "<td>&#xa0;</td>" & vbCrLf
    Next
  End If

  ' loop through the days in the current month
  For X = 1 to intNumberOfDays

    ' get the day we're working on
    strCurrentDay = CStr(intMonth) & "-" & X & "-" & CStr(intYear)
    
    
    ' start the table cell for a day
    Response.Write "<td id=""" & X & """ align=""left"" valign=""top"">"
    Response.Write "<a name=""" & X & """>" & X & "</a><br>" & vbCrLf
    
    ' *** NOTE - The following case section can be optional when database functionality is added
    ' ***  e.g. if no database record exists for a given day, then use the case section
    
    Select Case Weekday(strCurrentDay)
      Case 2
        Response.Write "<u>9:00am</u><br><i>Monday's activities</i><br>" & vbCrLf
      Case 3
        Response.Write "<u>9:00am</u><br><i>Tuesday's activities</i><br>" & vbCrLf
      Case 4
        Response.Write "<u>9:00am</u><br><i>Wednesday's activities</i><br>" & vbCrLf
      Case 5
        Response.Write "<u>9:00am</u><br><i>Thursday's activities</i><br>" & vbCrLf
      Case 6
        Response.Write "<u>9:00am</u><br><i>Friday's activities</i><br>" & vbCrLf
      Case Else
        For Y = 1 To 2
          Response.Write "<br><br>" & vbCrLf
        Next
    End Select

    ' *** end of case section


    ' end the table cell for a day
    Response.Write "</td>" & vbCrLf

    ' if the current day is then end of a week then output the end of the table row
    If (Weekday(strCurrentDay) = 7) And (strCurrentDay <> strLastDay) Then
      Response.Write "</tr>" & vbCrLf & "<tr>" & vbCrLf
    End If

  Next

  ' print empty table cells for the ending days not in the current month
  If (7-Weekday(strLastDay)) Then  
    For X = 1 to (7-Weekday(strLastDay))
      Response.Write "<td>&#xa0;</td>" & vbCrLf
    Next
  End If
%>
</tr>
</table>

<hr>

</body>
</html> 
Note that this ASP code could be modified to perform various functions, such as:
  • Display common daily events for a department


  • List the hours that a store is open


  • Integrate calendar events kept in a database


For more information on Microsoft scripting technologies, please see the Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/scripting/

Additional query words:

Keywords : kbDSupport kbiis500
Version : winnt:5.0
Platform : winnt
Issue type : kbhowto


Last Reviewed: December 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.