+ -
当前位置:首页 → 问答吧 → 亲,如何将ACCESS当中存在的日期反映在ASP页面当中也是为yyyy-mm-dd格式?

亲,如何将ACCESS当中存在的日期反映在ASP页面当中也是为yyyy-mm-dd格式?

时间:2011-12-03

来源:互联网

我在我的ACCESS数据库编码的时候把日期设定为DATE类型的,存进去的数据都是yyyy/mm/dd类型,我后来改了ACCESS常规-》格式:yyyy-mm-dd在ACCESS里面显示的是yyyy-mm-dd但是在页面里还是显示:yyyy/mm/dd。
F1 F1 F1 help help help 啊!亲们~谢谢了啦

作者: shaoxin0827   发布时间: 2011-12-03

http://w3school.com.cn/vbscript/func_formatdatetime.asp

FormatDateTime

作者: p2227   发布时间: 2011-12-03

1 最简单就用 Replace
response.write Replace(rs("date_field").value, "/", "-")

2 response.write fmtDate(rs("date_field").value)

Function fmtDate(d)
  If IsData(d) Then
  fmtDate = Year(d) & "-" & Month(d) & "-" & Day(d)
  Else
  fmtDate = ""
  End If
End Function

作者: hookee   发布时间: 2011-12-03

用下面这个函数吧,日期格式可以自己随便定义。
按你的要求这样调用:FormatDate(rs("日期"),%Y-%M-%D)

VBScript code

<%
'**********************************************
'名称:FormatDate
'作者:[email protected]
'日期:2011年11月26日
'说明:修改自国外某作者
'**********************************************
Function FormatDate(byVal strDate, byVal strFormat)
    ' strDate 必须是一个合法的日期格式

    ' %Y 四位数显示年份。例:1998
    ' %y 两位数显示年份。例:98
    ' %M 月份以数字显示,显示0。 例:02
    ' %m 月份以数字显示,不显示0。 例:2
    ' %D 日期以数字显示,显示0。例:09
    ' %d 日期以数字显示,不显示0。例:9
    ' %H 24小时制。例:23
    ' %h 12小时制。例:11
    ' %N 分钟。两位数显示。例:05
    ' %S 秒钟。两位数显示。例:09
    ' %W 星期用大写中文显示。
    ' %w 星期用整数显示。(7表示星期日)
    ' %T 年份的第几天。例:159
    ' %P 时间后缀。例:下午
    ' %CY 年份以中文显示。
    ' %Cy 年份以中文显示。
    ' %CM 月份以中文显示。
    ' %CD 日期以中文显示。

    On Error Resume Next

    Dim int12HourPart
    Dim str24HourPart
    Dim strMinutePart
    Dim strSecondPart
    Dim strAMPM
    Dim strNum

    strFormat = Replace(strFormat, "%Y", DatePart("yyyy", strDate), 1, -1, vbBinaryCompare)
    strFormat = Replace(strFormat, "%y", Right(DatePart("yyyy", strDate), 2), 1, -1, vbBinaryCompare)
    strFormat = Replace(strFormat, "%M", Right("0" & DatePart("m", strDate), 2), 1, -1, vbBinaryCompare)
    strFormat = Replace(strFormat, "%m", DatePart("m", strDate), 1, -1, vbBinaryCompare)
    strFormat = Replace(strFormat, "%D", Right("0" & DatePart("d", strDate), 2), 1, -1, vbBinaryCompare)
    strFormat = Replace(strFormat, "%d", DatePart("d", strDate), 1, -1, vbBinaryCompare)
    '%H 24小时制。
    str24HourPart = DatePart("h", strDate)
    If Len(str24HourPart) < 2 Then str24HourPart = "0" & str24HourPart
    strFormat = Replace(strFormat, "%H", str24HourPart, 1, -1, vbBinaryCompare)
    '%h 12小时制。
    int12HourPart = DatePart("h", strDate) Mod 12
    If int12HourPart = 0 Then int12HourPart = 12
    strFormat = Replace(strFormat, "%h", int12HourPart, 1, -1, vbBinaryCompare)
    '分钟。
    strMinutePart = DatePart("n", strDate)
    If Len(strMinutePart) < 2 Then strMinutePart = "0" & strMinutePart
    strFormat = Replace(strFormat, "%N", strMinutePart, 1, -1, vbBinaryCompare)
    '秒钟。
    strSecondPart = DatePart("s", strDate)
    If Len(strSecondPart) < 2 Then strSecondPart = "0" & strSecondPart
    strFormat = Replace(strFormat, "%S", strSecondPart, 1, -1, vbBinaryCompare)
    '星期
    strFormat = Replace(strFormat, "%w", WeekDay(strDate, vbMonday), 1, -1, vbBinaryCompare)
    strFormat = Replace(strFormat, "%W", Right(WeekDayName(WeekDay(strDate, 2),,2), 1), 1, -1, vbBinaryCompare)
    '年份的第几天。
    strFormat = Replace(strFormat, "%T", DatePart("y", strDate), 1, -1, vbBinaryCompare)
    '添加时间后缀
    If DatePart("h", strDate) >= 12 Then
        strAMPM = "下午"
    Else
        strAMPM = "上午"
    End If
    strFormat = Replace(strFormat, "%P", strAMPM, 1, -1, vbBinaryCompare)
    strNum = DatePart("yyyy", strDate)
    strFormat = Replace(strFormat, "%CY", NumToCN(strNum), 1, -1, vbBinaryCompare)
    strNum = Right(DatePart("yyyy", strDate), 2)
    strFormat = Replace(strFormat, "%Cy", NumToCN(strNum), 1, -1, vbBinaryCompare)
    strNum = DatePart("m", strDate)
    strFormat = Replace(strFormat, "%CM", IntToCN(strNum), 1, -1, vbBinaryCompare)
    strNum = DatePart("d", strDate)
    strFormat = Replace(strFormat, "%CD", IntToCN(strNum), 1, -1, vbBinaryCompare)

    FormatDate = strFormat
End Function

Function NumToCN(strNum)
    Dim strText
    strText = CStr(strNum)
    strText = Replace(strText, "1", "")
    strText = Replace(strText, "2", "")
    strText = Replace(strText, "3", "")
    strText = Replace(strText, "4", "")
    strText = Replace(strText, "5", "")
    strText = Replace(strText, "6", "")
    strText = Replace(strText, "7", "")
    strText = Replace(strText, "8", "")
    strText = Replace(strText, "9", "")
    strText = Replace(strText, "0", "")

    NumToCN = strText
End Function

Function IntToCN(strNum)
    Dim strText, intText
    strText = CStr(strNum)
    intText = CInt(strNum)
    If intText > 0 And intText < 10 Then
        strText = NumToCN(strText)
    ElseIf intText = 10 Then
        strText = ""
    ElseIf intText > 10 And intText < 20 Then
        strText = "" & NumToCN(Right(strText, 1))
    ElseIf intText >= 20 And intText Mod 10 = 0 Then
        strText = NumToCN(Left(strText, 1)) & ""
    ElseIf intText < 31 Then
        strText = NumToCN(Left(strText, 1)) & "" & NumToCN(Right(strText, 1))
    End If

    IntToCN = strText
End Function
%>

作者: netbooting   发布时间: 2011-12-03

上面写错了,没加引号,应该这样FormatDate(rs("日期"),"%Y-%M-%D")

作者: netbooting   发布时间: 2011-12-03