ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP Source Code:
<%
' ***Begin Function Declaration***
' New and improved GetDaysInMonth implementation.
' Thanks to Florent Renucci for pointing out that I
' could easily use the same method I used for the
' revised GetWeekdayMonthStartsOn function.
Function GetDaysInMonth(iMonth, iYear)
	Dim dTemp
	dTemp = DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1))
	GetDaysInMonth = Day(dTemp)
End Function
' Previous implementation of GetDaysInMonth
'Function GetDaysInMonth(iMonth, iYear)
'	Select Case iMonth
'		Case 1, 3, 5, 7, 8, 10, 12
'			GetDaysInMonth = 31
'		Case 4, 6, 9, 11
'			GetDaysInMonth = 30
'		Case 2
'			If IsDate("February 29, " & iYear) Then
'				GetDaysInMonth = 29
'			Else
'				GetDaysInMonth = 28
'			End If
'	End Select
'End Function
Function GetWeekdayMonthStartsOn(dAnyDayInTheMonth)
	Dim dTemp
	dTemp = DateAdd("d", -(Day(dAnyDayInTheMonth) - 1), dAnyDayInTheMonth)
	GetWeekdayMonthStartsOn = WeekDay(dTemp)
End Function
Function SubtractOneMonth(dDate)
	SubtractOneMonth = DateAdd("m", -1, dDate)
End Function
Function AddOneMonth(dDate)
	AddOneMonth = DateAdd("m", 1, dDate)
End Function
' ***End Function Declaration***
Dim dDate     ' Date we're displaying calendar for
Dim iDIM      ' Days In Month
Dim iDOW      ' Day Of Week that month starts on
Dim iCurrent  ' Variable we use to hold current day of month as we write table
Dim iPosition ' Variable we use to hold current position in table
Dim iLooper   ' Variable used for misc. loops
' Get selected date.  There are two ways to do this.
' First check if we were passed a full date in RQS("date").
' If so use it, if not look for seperate variables, putting them togeter into a date.
' Lastly check if the date is valid...if not use today
If IsDate(Request.QueryString("date")) Then
	dDate = CDate(Request.QueryString("date"))
Else
	If IsDate(Request.QueryString("month") & "-" & Request.QueryString("day") & "-" & Request.QueryString("year")) Then
		dDate = CDate(Request.QueryString("month") & "-" & Request.QueryString("day") & "-" & Request.QueryString("year"))
	Else
		dDate = Date()
		' The annoyingly bad solution for those of you running IIS3
		If Len(Request.QueryString("month")) <> 0 Or Len(Request.QueryString("day")) <> 0 Or Len(Request.QueryString("year")) <> 0 Or Len(Request.QueryString("date")) <> 0 Then
			Response.Write "The date you picked was not a valid date.  The calendar was set to today's date.<BR><BR>"
		End If
		' The elegant solution for those of you running IIS4
		'If Request.QueryString.Count <> 0 Then Response.Write "The date you picked was not a valid date.  The calendar was set to today's date.<BR><BR>"
	End If
End If
'Now we've got the date.  Now get Days in the choosen month and the day of the week it starts on.
iDIM = GetDaysInMonth(Month(dDate), Year(dDate))
iDOW = GetWeekdayMonthStartsOn(dDate)
%>
<!-- The outer table is simply to get the pretty border. -->
<table border="10" cellspacing="0" cellpadding="0">
<tr><td>
<table border="1" cellspacing="0" cellpadding="1" bgcolor="#99CCFF">
	<tr>
		<td bgcolor="#000099" align="center" colspan="7">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td align="right" ><a href="calendar.asp?date=<%= SubtractOneMonth(dDate) %>"><font color="#FFFF00" size="-1"><<</font></a></td>
					<td align="center"><font color="#FFFF00"><strong><%= MonthName(Month(dDate)) & "  " & Year(dDate) %></strong></font></td>
					<td align="left"  ><A HREF="calendar.asp?date=<%= AddOneMonth(dDate) %>"><font color="#FFFF00" size="-1">>></font></a></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Sun</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Mon</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Tue</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Wed</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Thu</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Fri</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
		<td align="center" bgcolor="#0000CC"><font color="#FFFF00"><strong>Sat</strong></font><br /><img src="images/spacer.gif" width="60" height="1" border="0"></td>
	</tr>
<%
' Write spacer cells at beginning of first row if month doesn't start on a Sunday.
If iDOW <> 1 Then
	Response.Write vbTab & "<tr>" & vbCrLf
	iPosition = 1
	Do While iPosition < iDOW
		Response.Write vbTab & vbTab & "<td> </td>" & vbCrLf
		iPosition = iPosition + 1
	Loop
End If
' Write days of month in proper day slots
iCurrent = 1
iPosition = iDOW
Do While iCurrent <= iDIM
	' If we're at the begginning of a row then write TR
	If iPosition = 1 Then
		Response.Write vbTab & "<tr>" & vbCrLf
	End If
	' If the day we're writing is the selected day then highlight it somehow.
	If iCurrent = Day(dDate) Then
		Response.Write vbTab & vbTab & "<td bgcolor=""#00FFFF""><font size=""-1""><strong>" & iCurrent & "</strong></font><br /><br /></td>" & vbCrLf
	Else
		Response.Write vbTab & vbTab & "<td><a href=""calendar.asp?date=" & Month(dDate) & "-" & iCurrent & "-" & Year(dDate) & """><font size=""-1"">" & iCurrent & "</font></a><br /><br /></td>" & vbCrLf
	End If
	' If we're at the endof a row then write /TR
	If iPosition = 7 Then
		Response.Write vbTab & "</tr>" & vbCrLf
		iPosition = 0
	End If
	' Increment variables
	iCurrent = iCurrent + 1
	iPosition = iPosition + 1
Loop
' Write spacer cells at end of last row if month doesn't end on a Saturday.
If iPosition <> 1 Then
	Do While iPosition <= 7
		Response.Write vbTab & vbTab & "<td> </td>" & vbCrLf
		iPosition = iPosition + 1
	Loop
	Response.Write vbTab & "</tr>" & vbCrLf
End If
%>
</table>
</td></tr>
</table>
<br />
<form action="calendar.asp" method="get">
<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="center">
			<select name="month">
			<%
			Response.Write vbCrLf
			For iLooper = 1 To 12
				Response.Write vbTab & vbTab & vbTab & vbTab
				Response.Write "<option value=""" & iLooper & """"
				If iLooper = Month(dDate) Then Response.Write " selected=""selected"""
				Response.Write ">"
				Response.Write MonthName(iLooper)
				Response.Write "</option>" & vbCrLf
			Next
			%>
			</select>
			<select name="day">
			<%
			For iLooper = 1 To 31
				%>
				<option<% If iLooper = Day(dDate) Then Response.Write " selected=""selected""" %>><%= iLooper %></option><%
			Next
			%>
			</select>
			<select name="year">
			<%
			For iLooper = Year(dDate) - 10 To Year(dDate) + 10
				%>
				<option<% If iLooper = Year(dDate) Then Response.Write " selected=""selected""" %>><%= iLooper %></option><%
			Next
			%>
			</select>
			<br />
			<input type="submit" value="Show Date on Calendar!" />
		</td>
	</tr>
</table>
</form>

Back the the Sample Output

Back to the Sample Index


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers