ASP+JS日历源代码

JavaScript08

ASP+JS日历源代码,第1张

直接保存成 asp文件 运行就可以

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<link href="http://purl.org/dc" rel="schema.DC" />

<title>日历</title

</head>

<body bgcolor="#FFFFFF">

<%

' 要调用的函数声明

'根据年份及月份得到每月的总天数

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

' 函数声明结束

Dim dDate ' 日历显示的日期

Dim iDOW ' 每一月开始的日期

Dim iCurrent ' 当前日期

Dim iPosition ' 表格中的当前位置

' 得到选择的日期并检查日期的合法性

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()

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 "您所选择的日期格式不正确,系统会使用当前日期.<BR><BR>"

End If

End If

End If

'得到日期后我们先得到这个月的天数及这个月的起始日期.

iDIM = GetDaysInMonth(Month(dDate), Year(dDate))

iDOW = GetWeekdayMonthStartsOn(dDate)

%>

<table width="180" height="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

<td><table width="150" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td height="5"></td>

</tr>

</table>

<table width="180" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td align="center" colspan="7"><table border="0" cellpadding="0" cellspacing="0"width="100%">

<tr>

<td height="22" align="right"><a href="rl.asp?date=<%= SubtractOneMonth(dDate) %>"><img src="../images/dot_left.gif" width="15" height="14" border="0" /></a></td>

<td align="center"><font color="999999"><b><%= MonthName(Month(dDate)) &" " &Year(dDate) %></b></font></td>

<td><a href="rl.asp?date=<%= AddOneMonth(dDate) %>"><img src="../images/dot_right.gif" width="15" height="14" border="0" /></a></td>

</tr>

</table></td>

</tr>

<tr>

<td width="25" height="22" align="center"><font

color="d08c00"><b>日</b></font></td>

<td width="25" align="center"><b><font color="999999">一</font></b></td>

<td width="25" align="center"><b><font color="999999">二</font></b></td>

<td width="25" align="center"><b><font color="999999">三</font></b></td>

<td width="25" align="center"><b><font color="999999">四</font></b></td>

<td width="25" align="center"><b><font color="999999">五</font></b></td>

<td width="25" align="center"><b><font color="d08c00">六</font></b></td>

</tr>

<%

' 如果这个月的起始日期不是周日的话就加空的单元.

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

' 绘制这个月的日历

iCurrent = 1

iPosition = iDOW

Do While iCurrent <= iDIM

' 如果是一行的开头就使用 TR 标记

If iPosition = 1 Then

Response.Write vbTab &"<TR>" &vbCrLf

End If

' 如果这一天是我们选择的日期就高亮度显示该日期.

If iCurrent = Day(dDate) Then

Response.Write vbTab &vbTab &"<TD BGCOLOR=#eeeeee height=18 align=center><B>" &iCurrent &"</B></TD>" &vbCrLf

Else

Response.Write vbTab &vbTab &"<TD height=18 align=center><A HREF=""./rl.asp?date=" &Month(dDate) &"-" &iCurrent &"-" &Year(dDate) &""">" &iCurrent &"</A></TD>" &vbCrLf

End If

' 如果满一周的话表格就另起一行

If iPosition = 7 Then

Response.Write vbTab &"</TR>" &vbCrLf

iPosition = 0

End If

iCurrent = iCurrent + 1

iPosition = iPosition + 1

Loop

' 如果一个月不是以周六结束则加上相应的空单元.

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>

<table width="150" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td height="5"></td>

</tr>

</table></td>

</tr>

</table>

首先必须解决的问题是表格的行与列问题。列是固定的,七列,因为一周有七天。行需要动态计算,因为,每一个月的第一天是星期几是一个变数,因而第一天在表格中的第几个单元也就跟着变化,同时,每个月的总天数不一致也影响着各个月对表格行数的需要量。

一. 表格的行数问题

1.首先取得处理月的总天数

JS不提供此参数,我们需要计算。考虑到闰年问题会影响二月份的天数,我们先编写一个判断闰年的自编函数:

function is_leap(year) {

return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0))

}

接着定义一个包含十二个月在内的月份总天数的数组:

m_days=new Array(31,28+is_leap(ynow),31,30,31,31,30,31,30,31,30,31)

m_days这个数组里,二月份的天数已经加入闰年的信息:28+is_leap(ynow)。数组元素从0开始,正好对应于JS提供的Date函数提供的getMonth返回值,即0表示一月,1表示二月,2表示三月,依此类推。

这样,各月总数可以这样取得:m_days[x]。其中,x为0至11的自然数。

2.计算处理月第一天是星期几

可以使用Date函数的getDay取得,返回的值从0到6,0表示星期一,1表示星期二,2表示星期三,其余依此类推。代码如下(假设要处理的时间为2008年3月):

n1str=new Date(2008,3,1)

firstday=n1str.getDay()

有了月总天数和该月第一天是星期几这两个已知条件,就可以解决表格所需行数问题:(当前月天数+第一天是星期几的数值)除以七。表格函数需要整数,因此,我们使用Math.ceil来处理:

tr_str=Math.ceil((m_days[mnow] + firstday)/7)

表格中的tr标签实际上代表表格的行,因此变量tr_str是我们往下写表格的重要依据。

二. 打印日历表格

可以使用两个for语句嵌套起来实现:外层for语句写行,内层for语句写单元格。

for(i=0i<tr_stri++) { //外层for语句 - tr标签

document.write("<tr>")

for(k=0k<7k++) { //内层for语句 - td标签

idx=i*7+k//表格单元的自然序号

date_str=idx-firstday+1//计算日期

//这里是处理有效日期代码

} //内层for语句结束

document.write("</tr>")

} //外层for语句结束

单元格的自然序号是否代表有效日期非常关键,为此必须加入一个过滤机制:仅打印有效的日期。有效的日期大于0小于小于等于处理月的总天数。