asp中怎么把YYYY-M-D转换成YYYY-MM-DD 用的Access?

2025-03-07 04:14:55
推荐回答(3个)
回答1:

我以前写的一个函数,里面有很多格式:
<%
'**************************************************
'函数名:GetFormatDate
'作 者:李茹星
'作 用:日期格式化
'**************************************************
Public Function GetFormatDate(DateAndTime, para)
On Error Resume Next
Dim y, m, d, h, mi, s, strDateTime
GetFormatDate = 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
GetFormatDate = strDateTime
End Function
%>

把上面函数包含在你的文件当中,调用如下:GetFormatDate(rs("dateandtime"),2)
里面还有很多格式的日期显示哦,希望对你有帮助!^_^

回答2:

使用vbscr提供的FormatDateTime函数,如下:

<%=FormatDateTime(rs("dateandtime"), 1) %>


后面参数的意思:
0 显示日期和/或时间。如果有日期部分,则将该部分显示为短日期格式。如果有时间部分,则将该部分显示为长时间格式。如果都存在,则显示所有部分。
1 使用计算机区域设置中指定的长日期格式显示日期。
2 使用计算机区域设置中指定的短日期格式显示日期。
3 使用计算机区域设置中指定的时间格式显示时间。
4 使用 24 小时格式 (hh:mm) 显示时间。

一般用0,1,3 你自己琢磨下。

回答3:

从ACCESS读取的:YYYY-M-D:2015-9-9
经过代码转换后:YYYY-MM-DD:2015-09-09
可以通过增加代码来实现此功能:
<%
Y=year(rs("dateandtime"))

M=month(rs("dateandtime"))
if m<10 then
M="0"&m
else
M=M
end if

D=day(rs("dateandtime"))
if D<10 then
D="0"&D
else
D=D
end if

Riqi=Y&"-"&M&"-"&D
%>