我能否覆盖Microsoft Access的默认日期显示,将年份小于50的日期显示为四位数,而不是两位数?

编程语言 2026-07-08

解决方案

将日期字段转换为字符串,使用 =Format([YourDateField], "yy"),以始终仅显示年份的后两位。

备选方案

你应该使用一个更准确的方法,例如下面这个函数,它返回的年龄是一个纯数字:

' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'   any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
    ByVal DateOfBirth As Date, _
    Optional ByVal AnotherDate As Variant) _
    As Integer

    Dim ThisDate    As Date
    Dim Years       As Integer

    If IsDate(AnotherDate) Then
        ThisDate = CDate(AnotherDate)
    Else
        ThisDate = Date
    End If

    ' Find difference in calendar years.
    Years = DateDiff("yyyy", DateOfBirth, ThisDate)
    If Years > 0 Then
        ' Decrease by 1 if current date is earlier than birthday of current year
        ' using DateDiff to ignore a time portion of DateOfBirth.
        If DateDiff("d", ThisDate, DateAdd("yyyy", Years, DateOfBirth)) > 0 Then
            Years = Years - 1
        End If
    ElseIf Years < 0 Then
        Years = 0
    End If

    Age = Years

End Function
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章