我正在尝试进行简单的日期差异计算,该计算返回“年”和“月”值。我现在的代码是:
Private Sub Form_AfterUpdate()
Dim IntegerYears As Long
Dim IntegerMonths As Long
IntegerYears = DateDiff("yyyy", Me.DeemedFilingDate, Now())
IntegerMonths = DateDiff("m", Me.DeemedFilingDate - IntegerYears, Now())
Me.TimeElapsedFromFilingDate = IntegerYears & " Yr(s)." & " " & "," & " " & IntegerMonths & " Mo(s)."
End Sub
此时,我正在以年/月格式获得输出,但这不正确。我得到负数,月份数超过12个月,年份显示1年,开始日期是前一年,但一年不一定过去。例如,如果开始日期是12/1/2017并且当前日期是3/2/2018而不是仅返回3个月,则代码将返回1年。
答案 0 :(得分:0)
解决:
Private Sub Form_AfterUpdate()
Dim TimeElapsedMonths As Integer
Dim TimeElapsedYears As Integer
Dim TimeElapsedRemMonths As Integer
TimeElapsedMonths = DateDiff("m", Me.DeemedFilingDate, Now())
TimeElapsedRemMonths = TimeElapsedMonths Mod 12
TimeElapsedYears = Int(TimeElapsedMonths / 12)
Me.TimeElapsedFromFilingDate = TimeElapsedYears & " Yr(s)." & "," & " " &
TimeElapsedRemMonths & " Mo(s)."
End Sub