function findMonth(fromDate,endDate)
interval=Datediff("m",fromDate,endDate)-1
response.write interval &"<br>"
if datepart("d",fromdate)=1 then
interval=interval+1
end if
if datepart("d",dateadd("d",1,enddate))=1 then
interval=interval+1
end if
findMonth=fromdate & "与" & enddate & "之间有" & interval & "个整月。" & "<br>"
end function
呵呵。。这些函数还是会用的。。
既然大家只给出这些函数,那我想看看大家对我编的2个针对我的要求写的代码,看看哪个效率高点。
<%
'******************************************************
cbegin_time="2003-1-2"
cend_time="2004-1-31"
'beginOfDays:cbegin_time到月底的时间,如果为0,整月+1
if day(cbegin_time)=1 then
beginOfDays = 0
else
beginOfDays =datediff("d",cbegin_time ,DATEADD("m", 1,cstr(year(cbegin_time))+"-"+cstr(month(cbegin_time))+"-1"))
end if
'beginTimeOfDays:cbegin_time所在的时间的该月的天数
beginTimeOfDays =day(DATEADD("d",-1,DATEADD("m", 1,cstr(year(cbegin_time))+"-" + cstr(month(cbegin_time))+"-1")))
'endOfdays: cend_time到月初的时间,如果为0,整月+1
if day(cend_time) = day(DATEADD("d",-1,DATEADD("m", 1,cstr(year(cend_time))+"-"+cstr(month(cend_time))+"-1"))) then
endOfdays = 0
else
endOfdays = day(cend_time)
end if
'如果numberOfMonths=-1说明2个时间在同一个月中,
endTimeOfDays = day( DATEADD("d" , -1 , DATEADD("m", 1,cstr(year(cbegin_time))+"-"+cstr(month(cbegin_time)) + "-1" )))
'如果numberOfMonths= 0说明2个时间在连续的两个月中.
numberOfMonths = datediff("M",cbegin_time,cend_time)-1
'numberOfDays:cbegin_time到cend_time的天数
numberOfDays = datediff("d",cbegin_time,cend_time)+1
if beginOfDays = 0 then
numberOfMonths=numberOfMonths+1
end if
if endOfdays = 0 then
numberOfMonths=numberOfMonths+1
end if
response.write cbegin_time &"到该月底有" &beginOfDays &"天<br>"
if beginOfDays = 0 then
response.write "这个月可以算成一个整月了。"
end if
response.write cend_time &"到该月首有" &endOfdays &"天<br>"
if endOfdays = 0 then
response.write "这个月可以算成一个整月了。"
end if
response.write cbegin_time &" 到 " &cend_time &"共有:" &numberOfMonths &"月"
strsql_time = " select case "
strsql_time= strsql_time &" when day('"&cbegin_time &"')=1 then 0 "
strsql_time= strsql_time &" else datediff(day,'"&cbegin_time &"',DATEADD(mm, 1,DATENAME (yy, '"&cbegin_time &"')+'-'+DATENAME (mm, '"&cbegin_time &"')+'-1')) "
strsql_time= strsql_time &" end as beginOfDays,"
strsql_time= strsql_time &" day(DATEADD(dd,-1,DATEADD(mm, 1,DATENAME (yy, '"&cbegin_time &"')+'-'+DATENAME (mm, '"&cbegin_time &"')+'-1'))) as beginTimeOfDays,"
strsql_time= strsql_time &" case "
strsql_time= strsql_time &" when day('" &cend_time &"')=day(DATEADD(dd,-1,DATEADD(mm, 1,DATENAME (yy, '" &cend_time &"')+'-'+DATENAME (mm, '" &cend_time &"')+'-1'))) then 0 "
strsql_time= strsql_time &" else day('" &cend_time &"')"
strsql_time= strsql_time &" end as endOfdays,"
strsql_time= strsql_time &" day(DATEADD(dd,-1,DATEADD(mm, 1,DATENAME (yy, '"&cend_time &"')+'-'+DATENAME (mm, '"&cend_time &"')+'-1'))) as endTimeOfDays,"
strsql_time= strsql_time &" datediff(MM,'"&cbegin_time &"','" &cend_time &"')-1 as numberOfMonths,"
strsql_time= strsql_time &" datediff(dd,'"&cbegin_time &"','" &cend_time &"')+1 as numberOfDays"
set rs2=cnn.execute(strsql_time)
beginOfDays = rs2("beginOfDays")
beginTimeOfDays = rs2("beginTimeOfDays")
endOfdays = rs2("endOfdays")
endTimeOfDays = rs2("endTimeOfDays")
numberOfMonths = rs2("numberOfMonths")
numberOfDays = rs2("numberOfDays")
if beginOfDays = 0 then
numberOfMonths=numberOfMonths+1
end if
if endOfdays = 0 then
numberOfMonths=numberOfMonths+1
end if
response.write cbegin_time &"到该月底有" &beginOfDays &"天<br>"
if beginOfDays = 0 then
response.write "这个月可以算成一个整月了。"
end if
response.write cend_time &"到该月首有" &endOfdays &"天<br>"
if endOfdays = 0 then
response.write "这个月可以算成一个整月了。"
end if
response.write cbegin_time &" 到 " &cend_time &"共有:" &numberOfMonths &"月"
'*******************************************************************
%>
一个是通过SQL来得到时间。
一个是用ASP直接得到时间。
哪个效率高呢?
(接上)
LCase()
FUNCTION: 返回字符串的小写形式
SYNTAX: Lcase(string)
ARGUMENTS: string is any valid string expression.
EXAMPLE: <%
strTest = "This is a test!"
response.write LCase(strTest)
%>
RESULT: this is a test!
LTrim()
FUNCTION: 去掉字符串左边的空格.
SYNTAX: LTrim(string)
ARGUMENTS:
EXAMPLE: <%
strTest = " This is a test!"
response.write LTrim(strTest)
%>
RESULT: This is a test!
Mid()
FUNCTION: 返回特定长度的字符串(从start开始,长度为length).
SYNTAX: Mid(string, start [, length])
ARGUMENTS:
EXAMPLE: <%
strTest = "This is a test! Today is Monday."
response.write Mid(strTest, 17, 5)
%>
RESULT: Today
Month()
FUNCTION: 返回日期.
SYNTAX: Month(date)
ARGUMENTS: date is any valid date expression.
EXAMPLE: <%=Month(#08/04/99#)%>
RESULT: 8
MonthName()
FUNCTION: Returns a string identifying the specified month.
SYNTAX: MonthName(month, [, Abb])
ARGUMENTS: month is the numeric representation for a given month; Abb
(optional) is a boolean value used to display month abbreviation. True
will display the abbreviated month name and False (default) will not show
the abbreviation.
EXAMPLE: <%=MonthName(Month(#08/04/99#))%>
RESULT: August
Now()
FUNCTION: Returns the current system date and time.
SYNTAX: Now()
ARGUMENTS: None
EXAMPLE: <%=Now%>
RESULT: 8/4/99 9:30:16 AM
Replace()
FUNCTION: Returns a string in which a specified sub-string has been
replaced with another substring a specified number of times.
SYNTAX: Replace(strToBeSearched, strSearchFor, strReplaceWith [, start
][, count ][, compare>])
ARGUMENTS: strToBeSearched is a string expression containing a sub-
string to be replaced; strSearchFor is the string expression to search for
within strToBeSearched; strReplaceWith is the string expression to replace
sub-string strSearchFor; start (optional) is the numeric character
position to begin search; count (optional) is a value indicating the
comparision constant.
EXAMPLE: <%
strTest = "This is an apple!"
response.write Replace(strTest, "apple", "orange")
%>
RESULT: This is an orange!
Rtrim()
FUNCTION: 去掉字符串右边的字符串.
SYNTAX: Rtrim(string)
ARGUMENTS:
EXAMPLE: <%
strTest = "This is a test!! "
response.write RTrim(strTest)
%>
RESULT: This is a test!!
Trim()
FUNCTION: 去掉字符串左右的空格.
SYNTAX: Trim(string)
ARGUMENTS: string is any valid string expression.
EXAMPLE: <%
strTest = " This is a test!! "
response.write Trim(strTest)
%>
RESULT: This is a test!!
UCase()
FUNCTION: 返回字符串的大写形式.
SYNTAX: UCase(string)
ARGUMENTS:
EXAMPLE: <%
strTest = "This is a test!!"
response.write UCase(strTest)
%>
RESULT: THIS IS A TEST!!
6.DateAdd()
FUNCTION: 返回一个被改变了的日期。
SYNTAX: DateAdd(timeinterval,number,date)
ARGUMENTS: timeinterval is the time interval to add; number is amount of
time intervals to add; and date is the starting date.
EXAMPLE: <%
currentDate = #8/4/99#
newDate = DateAdd("m",3,currentDate)
response.write newDate
%>
function findMonth(fromDate,endDate)
interval=Datediff("m",fromDate,endDate)
if datepart("d",fromdate)>1 then
interval=interval-1
end if
if datepart("d",dateadd("d",1,enddate))<>1 then
interval=interval-1
end if
findMonth=fromdate & "与" & enddate & "之间有" & interval & "个整月。" & "<br>"
end function
response.Write findMonth(fromdate,enddate)
function findDay(dt)
interval=Datediff("d",dt,DateAdd("m",1,dt))
interval=interval-Datepart("d",dt)
findDay=dt & "到月底还有" & interval & "天。" & "<br>"
end function