【问题标题】:VBA: function to get difference from birth date and other date using DateDiffVBA:使用DateDiff获取出生日期和其他日期差异的函数
【发布时间】:2020-03-22 12:42:50
【问题描述】:

为了得到两个 Dates 之间的日期差,我一头雾水,但一开始我总是遇到问题,例如:

如果要比较的两个值的月份相同,则结果为负数。 或者在某些情况下应该是2个月零30天的差异,它设法得到了3个月零1天的结果,如何?

一个例子如下: date1 = "2020 年 2 月 15 日" date2 = "2019 年 11 月 16 日"

我到底在哪里 月 = 3 天 = 1 为什么? 因为从 2019 年 11 月底到 2020 年 2 月,有 3 个月......而 day = 1 因为 16 - 15 是 1(?),如果日期是“2019 年 11 月 14 日”,你会得到一个: 天 = -1

最后我设法用下面的代码解决了我所有的问题:

Public Function GetDiffDate(birthdate As Date, otherDate As Date) As Variant
Dim CurrentDate, Years, ThisYear, Months, ThisMonth, Days

CurrentDate = CDate(birthdate )
Years = DateDiff("yyyy", CurrentDate, otherDate ) - 1
ThisYear = DateAdd("yyyy", Years, otherDate )
Months = DateDiff("m", ThisYear, otherDate )
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, otherDate )

Do While (Days < 0) Or (Days > 0 And Months = 12) Or (Months < 0) Or (Months = 12) Or (Years < 0)

'> Here I can deduce if the days are negative, if so, then reduce the
'> account by one month and re-draw the days of difference

If Days < 0 Then 
                If Months > 0 Then Months = Months - 1
                ThisMonth = DateAdd("m", Months, ThisYear)
                Days = DateDiff("d", ThisMonth, otherDate ) * -1
            End If
            If Months < 0 Then
                ThisYear = DateAdd("yyyy", Years, CurrentDate)
                Months = DateDiff("m", ThisYear, otherDate )
                ThisMonth = DateAdd("m", Months, ThisYear)
                Days = DateDiff("d", ThisMonth, otherDate )
            End If

            If Days > 0 And Months = 12 Then
                If Years >= 0 Then Years = Years + 1
                Months = 0
            ThisMonth = DateAdd("m", Months, ThisYear)
            End If

            If Months = 12 And Days = 0 Then
                    Years = Years + 1
                    Months = 0
            End If
        Loop
    End Function

示例
我犯的错误是这样的: 出生日期 = "02/15/2019" otherDate = "02/16/2020"

我得到这个代码:

Years = DateDiff ("yyyy", CurrentDate, otherDate)
     ThisYear = DateAdd ("yyyy", Years, otherDate)
     Months = DateDiff ("m", ThisYear, otherDate)
     ThisMonth = DateAdd ("m", Months, ThisYear)
     Days = DateDiff ("d", ThisMonth, otherDate)

结果:

年 = 1 个月 = 3 天 = -1

但真正的价值应该是

年 = 0,月 = 2,天 = 30

为此,我实现了我的 while do 和 if 条件来调整结果。 但我的问题是:

如果有其他方法可以让这更优雅?

感谢和问候!

【问题讨论】:

  • 你希望结果是什么,天数?
  • 我只需要数字@Bankar
  • Debug.Print DateDiff("d", #1/16/2019#, #2/15/2020#) 会给395
  • 你不能只减去日期吗?假设单元格 A1 = 2/15/2020,B1 = 11/16/2019。 A1-B1 给你 91,意思是相差 91 天
  • 对不起伙计,你完全失去了我,你能编辑问题以包含样本吗?预期的输出是什么?

标签: excel vba loops date datediff


【解决方案1】:

嗯,好的。您可以为此使用公式。假设单元格 A1 包含 2/15/2020,单元格 B1 包含 11/16/2019

To get the day only difference: =DATEDIF(B1,A1,"md")

To get the month only difference: =DATEDIF(B1,A1,"ym")

To get the year only difference: =DATEDIF(B1,A1,"y")

所以如果你想让它显示在一个单元格中,你可以像下面这样组合它们:

=DATEDIF(B1,A1,"md") & " day(s), "&DATEDIF(B1,A1,"ym")&" month(s), "&DATEDIF(B1,A1,"y")&" year(s)" 

This will output 30 day(s), 2 month(s), 0 year(s)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2013-01-11
    相关资源
    最近更新 更多