Calculating days between dates

Looking at the documentation I found an example on how to get days between dates

Var d1 As New DateTime(2018, 8, 1)
Var d2 As New DateTime(2015, 3, 1)
// Subtract seconds and convert seconds to days
Var days As Double = (d1.SecondsFrom1970 - d2.SecondsFrom1970) / (24 * 60 * 60)

I get a double with this amount: 1248.9583333333332575 this is due at the DST difference for 2015-03-01.

I searched this forum and in one they had the code above but instead of Double an Integer, so the result is 1248 with that code.

An internet site to calculate days between dates show 1249.

How do you handle this situation? Always Round or another way?

Rounding the double result would give you 1249, since the decimal is more than 0.5.

Correct.
I can’t think of a case that I can get less than 23/24 or more than 25/24, so round should always work.
I hope I’m not missing something.

Edit: changed the documentation code to:

Var d1 As New DateTime(2018, 8, 1)
Var d2 As New DateTime(2015, 3, 1)
// Subtract seconds and convert seconds to days
Var days As integer = round((d1.SecondsFrom1970 - d2.SecondsFrom1970) / (24 * 60 * 60))

I believe that if both dates are in the same time zone (other than DST type adjustments) you should be correct. If the UTC offsets are over 12 hours apart, then rounding seems like it would not always work.

You could also take the UTC time of each date and do the math on them. But then it may also depend on if you want the answer in absolute UTC days or if the result should be based on local times. For example, if sailing across the Pacific and your two dates are on just opposite sides of the International Date Line, is that one day apart or not? And the answer to that may depend on how the day count is to be used.

1 Like

Would DateInterval help here?

Var d1 As New DateTime(2017, 5, 1)
Var d2 As New DateTime(2017, 5, 20)
Var interval As DateInterval
interval = d2 - d1
// interval.Days = 19
1 Like

I wish but no:

Var d1 As New DateTime(2015, 3, 1)
Var d2 As New DateTime(2018, 8, 1)
Var interval As DateInterval
interval = d2 - d1
// interval.Days = 19
Var days As Integer = interval.Days
Var months As Integer = interval.Months
Var years As Integer = interval.Years

Days = 0
Months = 5
Years = 3
and we don’t know if the Months are 28/29/30/31 days and the years are all 365 or one at 366 there.

Do you think that a note for that example will make it clearer for users reading the documentation?

When I first read the interval days I expected to get the result I want and not 0.

DateInterval does consider those factors; the reason days = 0 in his example is he was using different dates for d1 and d2. Look again at his code and it is 2015-03-01 to 2018-08-1 which is in fact 3 years, 5 months, and 0 days.

I’ve never tested how it handles dates in two time zones that are 12 or more hours apart.

I don’t understand what are you trying to say.

What I’m trying to say is that DateInterval can’t give me TotalDays between dates.

Is great that DateInterval can say that the difference is 3 years, 5 months and 0 days, now, how do I convert that to total days from DateInterval? Are the 3 years 365 days each or there is a 366 year there? Are the 5 months 28,31,30,31,30 or 30,31,31,30,31? How do I convert that DateInterval to TotalDays and not years, months, days?

So no, DateInterval.Days will not give me the total number of days between dates. That’s what I was responding to Christoph.

1 Like