确切的date差异在几个月和几天

DateDiff计算不正确:

DateDiff("m", "30/06/2011", "24/06/2012") will return 12 

但我需要返回11,因为真正的差别是11个月和25天

也许任何人有这个问题的具体解决scheme

这对我来说将是理想的:11 个月和25

这是比上述定制function更清洁的解决scheme。 大多数情况下,它使用内置的DateDiff函数,但如果四舍五入,则会调整答案。 即使第一个date晚于第二个date,我的函数也会给你不同的结果,还可以添加一些文字说明。

 Function YearsMonthsDays(Date1 As Date, _ Date2 As Date, _ Optional ShowAll As Boolean = False, _ Optional Grammar As Boolean = True, _ Optional MinusText As String = "Minus " _ ) As String Dim dTempDate As Date Dim iYears As Integer Dim iMonths As Integer Dim iDays As Integer Dim sYears As String Dim sMonths As String Dim sDays As String Dim sGrammar(-1 To 0) As String Dim sMinusText As String If Grammar = True Then sGrammar(0) = "s" End If If Date1 > Date2 Then dTempDate = Date1 Date1 = Date2 Date2 = dTempDate sMinusText = MinusText End If iYears = DateDiff("yyyy", Date1, Date2) Date1 = DateAdd("yyyy", iYears, Date1) If Date1 > Date2 Then iYears = iYears - 1 Date1 = DateAdd("yyyy", -1, Date1) End If iMonths = DateDiff("M", Date1, Date2) Date1 = DateAdd("M", iMonths, Date1) If Date1 > Date2 Then iMonths = iMonths - 1 Date1 = DateAdd("m", -1, Date1) End If iDays = DateDiff("d", Date1, Date2) If ShowAll Or iYears > 0 Then sYears = iYears & " year" & sGrammar((iYears = 1)) & ", " End If If ShowAll Or iYears > 0 Or iMonths > 0 Then sMonths = iMonths & " month" & sGrammar((iMonths = 1)) & ", " End If sDays = iDays & " day" & sGrammar((iDays = 1)) YearsMonthsDays = sMinusText & sYears & sMonths & sDays End Function 

你不会用纯的DateDiff得到它。 如果你使用这个自定义function ,你会得到你想要的结果:

 0 years, 1 months, 1 days 

你也可以看看DateDif函数(最后单个f)。 请注意,DateDif()仍然会给出一些结果,当结束月份的天数less于开始月份时,可能会出现意想不到的结果(即负数天数)。