【问题标题】:If using DateDiff function, using the interval "yyyy", is the function simply subtracting the difference between the year value of date1 and date2?如果使用 DateDiff 函数,使用间隔“yyyy”,该函数是否只是减去 date1 和 date2 的年份值之间的差?
【发布时间】:2019-10-01 00:30:51
【问题描述】:

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

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

【问题讨论】:

  • 您不是要求它检查年龄是否超过 21 岁,而是检查他们出生的年份,但今年是否是该年之后的 21 年。 A = "01/04/1998"Msgbox DateAdd("yyyy", 21, A)If CDbl(DateAdd("yyyy", 21, A)) &lt; CDbl(Now()) then msgbox "Old Enough"
  • 在澳大利亚需要 18 岁。

标签: vba datediff


【解决方案1】:

更糟糕的是:
当比较 12 月 31 日和下一年的 1 月 1 日时,
DateDiff for Year ("yyyy") 返回 1,即使只过去了一天,Microsoft 表示。

所以你最好比较两个日期,例如。 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”开头有点误导。

【讨论】:

  • 非常感谢 Vielen Dank!
猜你喜欢
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
相关资源
最近更新 更多