Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display a Dynamic Calendar

HTML VB scripting

Display a Dynamic Calendar

by  markdmac  Posted    (Edited  )
markdmac's Enterprise Ready Scripts

While doing a recent web site redesign, I was looking for an easy to use ASP calendar. I found lots of examples, many used Java Script which I prefer to stay away from.

Many of the examples I found seemed to work "most of the time" but for some reason many seemed to have a problem with any month that started on a Saturday. Those that did pass my extensive checking work were just too plain. So, I decided to undertake the task of creating my own vbscript calendar. What I wanted was something simple and elegant.

So, after many painstaking hours, I came up with the following all VBScript solution.

This calendar will be dynamic, it will update the month and highlight the current day. I hope you can use it.

Just save the below code to an ASP file and pop it into your web server. The code below can be used in an INC file for easy adding to web pages.

If you find this FAQ useful, please rate it at the bottom of your screen.

Code:
[b]
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<body bgcolor="#FFFFD8">
<center><h1>MarkDMac's ASP Calendar Example</h1>
<%
'ASP Calendar by Mark D. MacLachlan, The Spider's Parlor
'URL http://www.thespidersparlor.com
'Creation Date 4/5/2004
'Copywrite (c)2004 All Rights Reserved

Dim DefaultCalendarBorderColor
Dim	i,x
Dim iDayToDisplay
Dim weekstogo
Dim borderSize
Dim borderColor

DefaultCalendarBorderColor = "#000000"
borderSize = 1
borderColor = DefaultCalendarBorderColor
highlightcolor = "#8FBACB"

thisday = Day(Date)
myDate =Date
myDay = DatePart("D", Date) 
iThisMonth = Month(Date)
iThisYear = Year(Date)
MyStartofMonth = (iThisMonth & "/1/" & iThisYear)
iThisMonthStartsThisDay=DatePart("W", MyStartofMonth) 

' Store the month names into an array.  Set element 0 to garbage so I don't have to do math later
Dim monthName
ReDim monthName(13)
	monthName(0) = "Space"
	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"



'specify the background colors for the cells if month starts on Saturday
'Dim monthStartbgcolor
'If (thisday = 1) Then
'	monthStartbgcolor="8FBACB"
'Else
'	monthStartbgcolor="FFFFFF"
'End If

'calculate Number of days this month
If iThisMonth <> 12 Then
	iDaysThisMonth = DateDiff("d",MyStartofMonth,((iThisMonth+1) & "/1/" & iThisYear))
Else
	iDaysThisMonth = DateDiff("d",MyStartofMonth,("1/1/" & (iThisYear+1)))
End If
%>
<table cellpadding="4" cellspacing="1" border="0" bgcolor="#ffffff"> 
					<tr><td colspan="7"align="center" bgcolor=<%Response.Write(Chr(34) & highlightcolor & Chr(34))%>><b>
					<%Response.Write (monthName(iThisMonth) & " " & iThisYear) %></b></td></tr>
	
<%	'Write the row of weekday initials...%>
<tr>
	<td align="center" bgcolor="#A1C6D1">S</td>
	<td align="center" bgcolor="#A1C6D1">M</td>
	<td align="center" bgcolor="#A1C6D1">T</td>
	<td align="center" bgcolor="#A1C6D1">W</td>
	<td align="center" bgcolor="#A1C6D1">T</td>
	<td align="center" bgcolor="#A1C6D1">F</td>
	<td align="center" bgcolor="#A1C6D1">S</td>
</tr>
<tr>
<%
'Now write the first row
For  i= 1 to 7
	if i=iThisMonthStartsThisDay Then 
	'       // start with the numeral 1.
		iDayToDisplay=1
	elseif i>iThisMonthStartsThisDay Then 
	' // increment the date
		iDayToDisplay = iDayToDisplay + 1
		
	else 
	'   // not first day yet? a non-breaking space
		iDayToDisplay="&nbsp;"
	end if
	if iDayToDisplay = thisDay Then
			Response.Write ("<td align=center bgcolor="& highlightcolor & "><b>" & iDayToDisplay & "</b></td>")

else
			Response.Write ("<td align=center>" & iDayToDisplay & "</td>") 
	end If
Next
%>
</tr>
<%
' 	Now, display the rest of the month.
' 	First figure out how many weeks are left to write

daysOffSet = 8-iThisMonthStartsThisDay
daysLeft = iDaysThisMonth-daysOffset

decWeekstogo = Round((daysLeft/7),2)

If decweekstogo > 4 Then
	weekstogo = 5
ElseIf decweekstogo > 3 and decweekstogo <= 4 Then
	weekstogo = 4
Else
	weekstogo = 3
End if

'Now write the rows and populate the data
For x = 1 To weekstogo
	Response.Write ("<tr>")
			For i=1 To 7
				If iDayToDisplay<iDaysThisMonth then
					iDayToDisplay=iDayToDisplay + 1
				else 
					iDayToDisplay="&nbsp;"
				End If
				if iDayToDisplay = thisDay then
							Response.Write ("<td align=center bgcolor=" & Chr(34)& highlightcolor & Chr(34) &"><b>" & iDayToDisplay & "</b></td>")
				else
							Response.Write ("<td align=center>" & iDayToDisplay & "</td>") 
				end If
			Next
	Response.Write ("</tr>")
	
	
Next
%>

</table>
</center>
</BODY>
</HTML>
[/b]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top