DateTime difference between 2 dates with result in number days

Trying to use the new DateTime class. Thought I read this somewhere where you can calculate the difference between two dates. I want to have a date in the future, compare to today’s date and calculate the number of days between. In older code, I was using the following, which does work

dim dtToday as new Date dim dtFuture as new Date dim daysRemaining as integer daysRemaining = ceil((dtFuture.TotalSeconds - dtToday.TotalSeconds)/(24*60*60))

…But I didn’t know if there is a newer method without using TotalSeconds with the new DateTime class. Thoughts? Keep using the above, or is there something cleaner?

Had I waited a few more minutes before posting, I would have found it in the docs :slight_smile:

http://documentation.xojo.com/api/data_types/datetime.html#datetime-secondsFrom1970

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

really thats a change without a meaningful difference from your original code (which still works FWIW)

It is. I guess I was just thinking there was a simpler approach like
future - today = result in number of days/weeks/months/whatever

But as you said, basically the same code

Why not use the DateInterval class? That’s what it’s designed to do.

you can simply subtract today from future and get a DateInterval object back

Var d1 As New DateTime(2018, 8, 1, TimeZone.Current)
Var d2 As New DateTime(2015, 3, 1, TimeZone.Current)

var interval as DateInterval = d1 - d2

its not substantially different though

Because it falls short in implementation with a lack of DateInterval.TotalDays DateInterval.TotalSeconds etc.

Is there a feedback ticket?

it is missing a number of convenience functions
Any thing beyond totalseconds would need to return a double I think
That way you could ask for totaldays and it might return 2.5 , or totalhours might be 1.5 or some other fractional amount

An old request from 2014 <https://xojo.com/issue/36887> where people don’t understand why its useful, I guess it was overlooked again in the new edition.

They would all return doubles.

Honestly, I wish xojo would just look around to see what is out there before they implement something as they frustratingly seem to miss the last 5% when doing things ( TimeSpan.TotalSeconds Property (System) | Microsoft Docs), I’m not sure if its an attempt to keep things “simple” or things aren’t planned and discussed, hopefully MVPs would catch things like this in the future.

But I digress…

I am actually getting errors from the code taken from the docs on these lines

Var d1 As New DateTime(2018, 8, 1, TimeZone.Current) Var d2 As New DateTime(2015, 3, 1, TimeZone.Current)

When I take out the TimeZone.Current, it will run without error. Is there something up with TimeZone.Current?

I got my code to work with

[code]Var d1 As DateTime = DateTime.FromString(TextField1.Text)
Var d2 As DateTime = DateTime.Now

var interval as DateInterval = d1 - d2

MessageBox interval.Days.ToString[/code]

But when I test it out and put 2020-02-22 (tomorrow), the result is zero. Shouldn’t that be “1”? Putting in 2020-02-23 (2 days from now) results in 1.

I may just end up still using the original code since that produced better results than the DateInterval. Am I missing something?

Xojo 2019r2.1

That’s what I experience too. But in 2019r3. Take out the TimeZone.Current and see if it runs. Mine did

My license expired 23rd november just before r3.
I payed $700 for a pro license and in that year 2019/2020 r2 totaly ruined a several months taking project (chart program, rather complex … at least for me) and Xojo delivered in that year only a pile of junk.

[quote=476271:@Ryan Hartz]
I may just end up still using the original code since that produced better results than the DateInterval. Am I missing something?[/quote]
You sure youre not getting something also including part days (like hours min seconds etc as well)

Sympathies

I was thinking that. Being that it’s 5pm here on the 21st, and if the date interval is checking the difference between today (which is almost over) and tomorrow starts at 00:00:00, then I could see tomorrow being zero. But I would think that if I request interval.days that it would only look at days and not the time of the day. Difference between today and tomorrow should be one day.

Is that what you meant?

.Days only returns the days component as tomorrow is less than a day away, it should be 0 as its an integer, its less then a 1 day away, and the rest of the properties, hours, minutes etc will contain the remainder. If .TotalDays was implemented it would return a decimal of the partial amount. If you want to check this interval using just days then check everything against 00:00:00 of the dates in question, this is essentially what you’re doing with the ceil in your original code.

[quote=476271:@Ryan Hartz]I am actually getting errors from the code taken from the docs on these lines

Var d1 As New DateTime(2018, 8, 1, TimeZone.Current) Var d2 As New DateTime(2015, 3, 1, TimeZone.Current)

When I take out the TimeZone.Current, it will run without error. Is there something up with TimeZone.Current?[/quote]

Nope
But there IS something up with the constructor
Gotta love optional params that arent optional :stuck_out_tongue: (its a long standing behaviour of “optional” parameters see Optional — Xojo documentation)

Youre providing an int, int, int, timezone and there is NO matching constructor :slight_smile:
its

Constructor(Year as Integer, Month as Integer, Day as Integer, hour as Integer = 0, minute as Integer = 0, second as Integer = 0, nanosecond as Integer = 0, timeZone as TimeZone = Nil)

and in this style to pass a timezone you would also need to pass hour, minute, second and nanoeseconds

maybe they need another constructor like

Constructor(Year as Integer, Month as Integer, Day as Integer, timeZone as TimeZone = Nil)

so your code could work

edit : fwiw I made a feature request to support truly optional params (VB 6 style where you could just have commas with no value supplied)
<https://xojo.com/issue/59088>

edit 2: and one for more constructors on date time so your code could work without them having to alter the compiler (this one is nearly as trivial to implement as it gets)
<https://xojo.com/issue/59269>

[quote=476278:@Ryan Hartz]I was thinking that. Being that it’s 5pm here on the 21st, and if the date interval is checking the difference between today (which is almost over) and tomorrow starts at 00:00:00, then I could see tomorrow being zero. But I would think that if I request interval.days that it would only look at days and not the time of the day. Difference between today and tomorrow should be one day.

Is that what you meant?[/quote]

Yes more or less