Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

VBA: function to get difference from birth date and other date using DateDiff

$
0
0

I broke my head to get the date difference between two Dates, but at the beginning I had constant problems, for example:

If the month was the same in both values ​​to be compared, the result was the negative days. Or in some cases where it should be differences of 2 months and 30 days, it managed to have results like 3 months and 1 days, how?

well an example would be the following: date1 = "02/15/2020" date2 = "11/16/2019"

where in the end I got month = 3 day = 1 why? because at the end of 11 from 2019 to 2 of 2020 there are 3 months ... and day = 1 because of 16 - 15 are 1 (?) and if the date was a "11/14/2019" you would get a: day = -1

In the end I managed to solve all my problems with the following code:

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

Example
The mistakes I had were like this: birthDate = "02/15/2019" otherDate = "02/16/2020"

with this code i get:

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)

Results:

Years = 1 Months = 3 Days = -1

but the real value should be

Years = 0, Months = 2, Days = 30

For this I implemented my while do and if conditions to adjust the result as it should be. But my question is:

If there is another way to make this more elegant?

I appreciate it and greetings!


Viewing all articles
Browse latest Browse all 88868


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>