ASP (VBScript) Calendar

This is a VBScript translation of the w3-msql "Lite" cgi calendar elsewhere on my site.  Note that the page keeps resubmitting to itself with different values, depending on the month that is to be displayed.  This was one of my first attempts at VBScript, so I am sure that the code could have been more economically written.

CODE


<%

Dim currDate, nMonth, nDay, nYear
Dim lastMonth, lastYear, lastFirstDayPos, lastNumDays, submit
Dim numDays, monthName(13), count, pos, firstDayPos, eventDay, dayNum, cell(38)
Dim events(38)
Dim test1, test2, test3 ' used in leap year calcs

If Request.Form("submit") <> "" Then

lastMonth = Int(Request.Form("lastMonth"))
lastYear = Int(Request.Form("lastYear"))
lastFirstDayPos = Int(Request.Form("lastFirstDayPos"))
lastNumDays = Int(Request.Form("lastNumDays"))
submit = Request.Form("submit")

End If

currDate = Date ' Date contains the current system date.
nMonth = Int(Month(currDate)) ' integer value 1 to 12
nDay = Int(Day(currDate)) ' integer value 1 to 31
nYear = Int(Year(currDate)) ' integer value yyyy
dayOfWeek = Int(Weekday(currDate)) ' integer values 1 to 7 (note diff from mini-SQL 0-6)

%>

<HTML>
<HEAD>
<TITLE>Calendar</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff" TEXT="#00000">
<CENTER>
<TABLE BORDER CELLPADDING="2">

<%

' Previous or Next Month (passed values)

If submit = "Previous Month" Then

nMonth = lastMonth - 1
nYear = lastYear -1
If nMonth = 0 Then
nYear = nYear - 1
nMonth = 12

End If

If submit = "Previous Month" Then

nMonth = lastMonth - 1
nYear = lastYear -1
If nMonth = 0 Then
nYear = nYear - 1
nMonth = 12

End If

If submit = "Next Month" Then

nMonth = lastMonth + 1
nYear = lastYear
If nMonth = 13 Then
nYear = nYear + 1
nMonth = 1

End If

' Number of days of month 30 or 31, with special case of February (28 or 29)

If nMonth = 4 Or nMonth = 6 Or nMonth = 9 Or nMonth = 11 Then
          numDays = 30
ElseIf nMonth <> 2 Then
          numDays = 31
End If

If nMonth = 2 Then

' Leap year calculations:
' All years that are evenly divisible by 400 or are evenly
' divisible by 4 and NOT divisible by 100 are leap years

test1 = nYear Mod 400
If test1 = 0 Then
          numDays = 29
Else
          test2 = nYear Mod 4
          test3 = nYear Mod 100
          If test2 = 0 And test3 > 0 Then
                    numDays = 29
          Else
                    numDays = 28
          End If
End If

End If

' Make month name array

monthName(1) = "January"
monthName(2) = "February"
monthName(3) = "March"
monthName(4) = "April"
monthName(5) = "May"
monthName(6) = "June"
monthName(7) = "July"
monthName(8) = "August"
monthName(9) = "September"
monthName(10) = "October"
monthName(11) = "November"
monthName(12) = "December"

' Find day of week (0-6) of first day of month. firstDayPos will
' help determine later display of cells.

If nMonth = Month(currDate) Then

          ' Current Month

count = nDay
pos = dayOfWeek - 1 ' subtract one for VB offset
While count > 0
          If count = 1 Then ' must come before pos switch to preserve assignment
                    firstDayPos = pos
          End If
          If pos = 0 Then ' must come before decrement, since Sunday = 0
                    pos = 7 ' must be 7 so that next loop is 6
          End If
          count = count - 1
          pos = pos - 1
Wend

End If

' Get new first day position depending on whether prev or next month
' has 31, 30, 29, or 28 days. Cannot be calculated from current
' time.

If submit = "Previous Month" Then

firstDayPos = lastFirstDayPos - numDays + 28
If firstDayPos < 0 Then
          firstDayPos = firstDayPos + 7
End If

End If

 If submit = "Next Month" Then

firstDayPos = lastFirstDayPos + lastNumDays - 28
If firstDayPos > 6 Then
          firstDayPos = firstDayPos - 7
End If

End If

' Prepare events for insertion

eventDay = Day(currDate)

' Make cell array

count = 0
dayNum = 1
While count <= numDays + firstDayPos ' Add number of initial blank days to count

If count < firstDayPos Then
          ' Empty cells display as solid blank
          cell(count) = "<td></td>"
Else
          ' Create empty event array

events(dayNum) = ""
If nMonth = Month(currDate) And nYear = Year(currDate) And eventDay = dayNum Then
          ' Assign check to day
          events(eventDay) = "<IMG SRC=" & Chr(34) & "check7.gif" & Chr(34) & ">"
End If
cell(count) = "<TD align=left valign=top>" & dayNum & "" & events(dayNum) _
          & "</td>"
dayNum = dayNum + 1

End If
count = count + 1

Wend

' Print the calendar elements. Table tags are above and below this in the HTML.
' Beginning first row tag and ending last row tag are outside the loop.

Response.Write "<CAPTION><FONT size=5>" & monthName(nMonth) & " " & nYear & "</FONT></CAPTION>"
Response.Write "<TR>"
Response.Write "<TD align=center> Sunday </TD>"
Response.Write "<TD align=center> Monday </TD>"
Response.Write "<TD align=center> Tuesday </TD>"
Response.Write "<TD align=center> Wednesday </TD>"
Response.Write "<TD align=center> Thursday </TD>"
Response.Write "<TD align=center> Friday </TD>"
Response.Write "<TD align=center> Saturday </TD>"
Response.Write "</TR>"
Response.Write "<TR>"

count = 0
While count < numDays + firstDayPos

Response.Write "" & cell(count)

' Insert subsequent table row tags in appropriate place after cell print

If count = 6 Or count = 13 Or count = 20 Or count = 27 Or count = 34 Then

Response.Write "<TR></TR>" ' Empty initial cells will display solid blank

End If

count = count + 1

' Final unprinted empty cells will display solid blank

Wend

Response.Write "</TR></TABLE></CENTER>"
Response.Write "<center><form action=" & Chr(34) & "calendar.asp" & Chr(34) & "method=""post"">"
Response.Write "<input type=hidden name=lastMonth value=" & nMonth & ">"
Response.Write "<input type=hidden name=lastYear value=" & nYear & ">"
Response.Write "<input type=hidden name=lastFirstDayPos value=" & firstDayPos & ">"
Response.Write "<input type=hidden name=lastNumDays value=" & numDays & ">"
Response.Write "<input type=submit name=submit value=" & Chr(34) & "Previous Month" _
          & Chr(34) & ">"
Response.Write "<input type=submit name=submit value=" & Chr(34) & "Next Month" _
          & Chr(34) & "></center>"

%>

 <p>This calendar uses NT system clock values to enter at the
current month and to place a check mark at the current day.
The "Previous Month" and "Next Month" submit buttons then
resubmit values from which the previous or next month's calendar is
calculated. The script is written in VBScript Active Server Pages (ASP),
translated from mini-SQL's Lite scripting language.</p>

<p align="center">

<%

Response.Write "You are using "
Response.Write ScriptEngine
Response.Write " version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion
Response.Write "&nbsp;&nbsp;&nbsp; Date: " & currDate

%>

</p>
</BODY>
</HTML>


| mSQL Calendar | Programming |

 

E-mail Pete the answers to all his questions.