DateTime in 2019r2

I’m trying to figure out how to get the interval between two dates using the new DateTime items in 2019r2. With the Xojo.Core.Date objects, you could add or subtract them but that’s not available with DateTime n 2019r2. I’d rather not have to break them into the respective atoms and do all the calculations myself. Is there a way to easily calculate the interval between two DateTime objects?

yeah there is no operator_subtract on them but it would be damned handy
we were discussing this offlist
<https://xojo.com/issue/57804>

and there’s no dateinterval constructor that lets you easily do

Dim di As new dateinterval(d2.SecondsFrom1970 - d1.SecondsFrom1970)

And I’m not sure if you do

Dim di As new dateinterval(d2.year - d1.year, d2,month - d1.month, d2.day - d1.day, d2.hour - d1.hour, d2.minute - d1.minute, d2.second - d1.second)

and you get negative values in any compnent what the behaviour will be

EDIT : ugh
ok if you do

Dim d1 As New datetime(DateTime.Now.SecondsFrom1970(), TimeZone.Current)
Dim d2 As New datetime(DateTime.Now.SecondsFrom1970()+24*60*60, TimeZone.Current)

Dim di As New dateinterval(d1.year - d2.year, d1.Month - d2.month, d1.day - d2.day, d1.hour - d2.hour, d1.minute - d2.minute, d1.Second - d2.second, d1.Nanosecond - d2.Nanosecond)

Dim d3 As New datetime(d1.SecondsFrom1970, TimeZone.Current)
d3 = d3 - di
Break

the date interval is created in a way that you HAVE to use it in the exact same way it was initially created
what I mean is IF you create the interval by subtracting a date further in the past from a newer one you get an interval that has to be subtracted from whatever other date (it basically stores an absolute difference)

if in the above code you made

d3 = d3 + di

you would get the wrong result
d3 would not be the same as D2

Need to check for relationship between the two dates:
Dim d1 As New datetime(DateTime.Now.SecondsFrom1970()+246060, TimeZone.Current)
Dim d2 As New datetime(DateTime.Now.SecondsFrom1970(), TimeZone.Current)

If d1 > d2 Then
Dim di As New dateinterval(d1.year - d2.year, d1.Month - d2.month, d1.day - d2.day, d1.hour - d2.hour, d1.minute - d2.minute, d1.Second - d2.second, d1.Nanosecond - d2.Nanosecond)

Dim d3 As New datetime(d1.SecondsFrom1970, TimeZone.Current)
d3 = d3 - di
MessageBox("The new date is " + d3.ToString)
MessageBox(“The date interval is " + di.Years.ToText + " yrs " + di.Months.ToText + " months " + di.Days.ToText + " days " + di.Hours.ToText + " hours " + di.Minutes.ToText + " mins " + di.Seconds.ToText + " secs " + di.Nanoseconds.ToText + " nanoseconds”)

End If

The date.dateinterval is a very nice way to do the sums. I suppose if one was just interested in the number of days, could check that the years are the same and then subtract the earlier dayofyear from the later dayofyear .

or,

failing my inability to use extend, here is a function;

Shared Function dateDifference(d1 As dateTime, d2 As dateTime) As DateInterval

// need to make sure that the earlier date is subtracted from the later date

If d2 >= d1 Then
Var Di As New dateinterval(d2.year - d1.year, d2.Month - d1.month, d2.day - d1.day, d2.ho ur - d1.hour, d2.minute - d1.minute, d2.Second - d1.second, d2.Nanosecond - d1.Nanose cond)
Return Di
Else //d2<d1
Var Di As New dateinterval(d1.year - d2.year, d1.Month - d2.month, d1.day - d2.day, d1.ho ur - d2.hour, d1.minute - d2.minute, d1.Second - d2.second, d1.Nanosecond - d2.Nanose cond)
Return Di
End If

Called as follows:

Var d1 As New datetime(DateTime.Now.SecondsFrom1970(), TimeZone.Current)
Var d2 As New datetime(DateTime.Now.SecondsFrom1970()+246060, TimeZone.Current)
Var d4 As New datetime(d1.SecondsFrom1970, TimeZone.Current)

d4 = d4-dateDifference(d1,d2) // note, order no longer matters

MessageBox("The new date is " + d4.ToString)

I hope that helps, and thanks to Norman Palardy for his insight