×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBScript FAQ

HTML VB scripting

Display a Dynamic Calendar by markdmac
Posted: 7 Apr 04 (Edited 13 Jun 12)

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


<%@ 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=" "
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=" "
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>

Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close