asp日期函数的应用(自己整理)
时间:2010-04-17 来源:mx11
将下面的代码复制保存到asp中就可以看到效果与调用方法
<%=now()%>
<br><br>
<%=Replace(Now(),":",":")%>
<br><br>
<hr>
获取当前系统日期和时间,ASP输出可以这样写:<%=now()%>
<br><br>
Year(now()) 获取年份, ASP输出:<%=Year(now())%>
<br><br>
Month(now()) 获取当前月份,ASP输出:<%=Month(now())%>
<br><br>
day(now()) 获取当天数,ASP输出:<%=day(now())%>
<br><br>
Minute(now()) 获取分钟数,ASP输出:<%=Minute(now())%>
<br><br>
Second(now()) 获取秒钟数,ASP输出:<%=Second(now())%>
<br><br>
<hr>
date() 获取当前系统日期,格式为:2004-2-28
<br><br>
time() 获取当前系统时间,格式为:22:24:59
<br><br>
函数调用格式:FormatDateTime(Date[, NamedFormat])
<br><br>
FormatDateTime函数的语法有以下参数:
<br><br>
参数 描述
<br><br>
Date 必选,要被格式化的日期表达式。
<br><br>
NamedFormat 可选,指示所使用的日期/时间格式的数值,如果省略,则使用vbGeneralDate。
<br><br>
<hr>
设置
<br><br>
NamedFormat参数可以有以下值:
<br><br>
vbGeneralDate 0 显示日期和/或时间;如果有日期部分,则将该部分显示为短日期格式;如果有时间部
<br><br>
分,则将该部分显示为长时间格式;如果都存在,则显示所有部分。
<br><br>
vbLongDate 1 使用计算机区域设置中指定的长日期格式显示日期。
<br><br>
vbShortDate 2 使用计算机区域设置中指定的短日期格式显示日期。
<br><br>
vbLongTime 3 使用计算机区域设置中指定的时间格式显示时间。
<br><br>
vbShortTime 4 使用24小时格式(hh:mm)显示时间。
<br><br>
<hr>
示例:
<br><br>
<%
response.write formatdatetime(now,0)&"<br>"
response.write formatdatetime(now,1)&"<br>"
response.write formatdatetime(now,2)&"<br>"
response.write formatdatetime(now,3)&"<br>"
response.write formatdatetime(now,4)&"<br>"
%>
<hr>
显示如下:
<br><br>
2007-9-5 1:52:27
<br><br>
2007年9月5日
<br><br>
2007-9-5
<br><br>
1:52:27
<br><br>
01:52
<br><br>
<%
'月份转换到中文
Function MonthToCH(TheMonth)
Dim mm
mm=split("一,二,三,四,五,六,七,八,九,十,十一,十二",",")
If IsNumeric(TheMonth) Then
MonthToCH = mm(TheMonth-1) & "月份"
Else
MonthToCH = " 月份"
End If
End Function
'月份转换到长英文
Function MonthToLongEN(TheMonth)
Dim mm
mm=split("January,February,March,April,May,June,July,August,September,October,November,December",",")
If IsNumeric(TheMonth) Then
MonthToLongEN = mm(TheMonth-1)
Else
MonthToLongEN = " "
End If
End Function
'月份转换到短英文
Function MonthToShortEN(TheMonth)
Dim mm
mm=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",",")
If IsNumeric(TheMonth) Then
MonthToShortEN = mm(TheMonth-1)
Else
MonthToShortEN = " "
End If
End Function
%>
<br><br>
获取当前月份,ASP输出:<%=MonthToCH(Month(now()))%>
<br><br>
<br><br>
获取当前月份,ASP输出:<%=MonthToLongEN(Month(now()))%>
<br><br>
<br><br>
获取当前月份,ASP输出:<%=MonthToShortEN(Month(now()))%>
<br><br>
<%
'================================================
'函数名:FormatDate
'作 用:格式化日期
'参 数:DateAndTime ----原日期和时间
' para ----日期格式
'返回值:格式化后的日期
'================================================
Public Function FormatDate(DateAndTime, para)
On Error Resume Next
Dim y, m, d, h, mi, s, strDateTime
FormatDate = DateAndTime
If Not IsNumeric(para) Then Exit Function
If Not IsDate(DateAndTime) Then Exit Function
y = CStr(Year(DateAndTime))
m = CStr(Month(DateAndTime))
If Len(m) = 1 Then m = "0" & m
d = CStr(Day(DateAndTime))
If Len(d) = 1 Then d = "0" & d
h = CStr(Hour(DateAndTime))
If Len(h) = 1 Then h = "0" & h
mi = CStr(Minute(DateAndTime))
If Len(mi) = 1 Then mi = "0" & mi
s = CStr(Second(DateAndTime))
If Len(s) = 1 Then s = "0" & s
Select Case para
Case "1"
'显示格式:09年07月06日 13:44
strDateTime = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case "2"
'显示格式:2009-07-06
strDateTime = y & "-" & m & "-" & d
Case "3"
'显示格式:2009/07/06
strDateTime = y & "/" & m & "/" & d
Case "4"
'显示格式:2009年07月06日
strDateTime = y & "年" & m & "月" & d & "日"
Case "5"
'显示格式:07-06 13:45
strDateTime = m & "-" & d & " " & h & ":" & mi
Case "6"
'显示格式:07/06
strDateTime = m & "/" & d
Case "7"
'显示格式:07月06日
strDateTime = m & "月" & d & "日"
Case "8"
'显示格式:2009年07月
strDateTime = y & "年" & m & "月"
Case "9"
'显示格式:2009-07
strDateTime = y & "-" & m
Case "10"
'显示格式:2009/07
strDateTime = y & "/" & m
Case "11"
'显示格式:09年07月06日 13:45
strDateTime = right(y,2) & "年" &m & "月" & d & "日 " & h & ":" & mi
Case "12"
'显示格式:09-07-06
strDateTime = right(y,2) & "-" &m & "-" & d
Case "13"
'显示格式:07-06
strDateTime = m & "-" & d
Case "14"
'显示格式:13:45
strDateTime = h & ":" & mi
Case Else
strDateTime = DateAndTime
End Select
FormatDate = strDateTime
End Function
%>
<br><br>
获取当前月份,ASP输出:<%=FormatDate(now(),13)%>
<br><br>
<br><br>
获取当前月份,ASP输出:<%=FormatDate(now(),11)%>
<br><br>
<hr>
把这段代码放入你的Function里就可以直接用了
ASP里格式化日期
复制代码 代码如下:
<%
' ============================================
' 格式化时间(显示)
' 参数:n_Flag
' 1:"yyyy-mm-dd hh:mm:ss"
' 2:"yyyy-mm-dd"
' 3:"hh:mm:ss"
' 4:"yyyy年mm月dd日"
' 5:"yyyymmdd"
' 6:"yyyymmddhhmmss"
' ============================================
Function Format_Time(s_Time, n_Flag)
Dim y, m, d, h, mi, s
Format_Time = ""
If IsDate(s_Time) = False Then Exit Function
y = cstr(year(s_Time))
m = cstr(month(s_Time))
If len(m) = 1 Then m = "0" & m
d = cstr(day(s_Time))
If len(d) = 1 Then d = "0" & d
h = cstr(hour(s_Time))
If len(h) = 1 Then h = "0" & h
mi = cstr(minute(s_Time))
If len(mi) = 1 Then mi = "0" & mi
s = cstr(second(s_Time))
If len(s) = 1 Then s = "0" & s
Select Case n_Flag
Case 1
' yyyy-mm-dd hh:mm:ss
Format_Time = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case 2
' yyyy-mm-dd
Format_Time = y & "-" & m & "-" & d
Case 3
' hh:mm:ss
Format_Time = h & ":" & mi & ":" & s
Case 4
' yyyy年mm月dd日
Format_Time = y & "年" & m & "月" & d & "日"
Case 5
' yyyymmdd
Format_Time = y & m & d
case 6
'yyyymmddhhmmss
format_time= y & m & d & h & mi & s
End Select
End Function
%>