如果使用 DateDiff 函数,使用区间 "yyyy",函数是否只是简单地减去 date1 和 date2 的年份值之间的差值?

If using DateDiff function, using the interval "yyyy", is the function simply subtracting the difference between the year value of date1 and date2?

宏在计算一个人的年龄时似乎没有考虑天数。

Sub alcohol_test()
    Dim strBirthday As Date

    strBirthday = CDate(InputBox("Input date of birth to be verified: ", "Date of Birth"))
    If DateDiff("yyyy", strBirthday, Date) < 21 Then MsgBox ("Customer underage, sale of alcohol illegal.") _
    Else MsgBox ("Age Confirmed: Alcohol may be sold")

End Sub

更糟的是:
将 12 月 31 日与下一年的 1 月 1 日进行比较时,
DateDiff 年 ("yyyy") returns 1,即使只过去了一天,说 Microsoft

所以你最好比较两个日期,e。 G。如果生日早于21年前

Dim datBirthday as Date
datBirthday = CDate(InputBox("Input date of birth to be verified: ", "Date of Birth"))
If datBirthday < DateSerial(Year(Date) - 21, Month(Date), Day(Date)) Then

我更改了变量名称,因为以 "str" 开头在您使用日期值时有点误导。