229 views

# DateTime difference between 2 dates with result in number days

1. 7 weeks ago

### Ryan H

Feb 21 XDC Speakers

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?

See post in context

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

http://docs.xojo.com/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)```
2. ### Ryan H

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

http://docs.xojo.com/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)```
3. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

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

4. ### Ryan H

Feb 21 XDC Speakers

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

5. ### Bob K

Feb 21 Pre-Release Testers, Xojo Pro Kansas City

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

6. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air
Edited 7 weeks ago

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

7. ### Julian S

Feb 21 Pre-Release Testers, Xojo Pro UK

@Bob K Why not use the DateInterval class? That's what it's designed to do.

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

8. ### Tim P

Feb 21 Pre-Release Testers, Xojo Pro Rochester, NY

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

Is there a feedback ticket?

9. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

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

10. ### Julian S

Feb 21 Pre-Release Testers, Xojo Pro UK

@Tim P Is there a feedback ticket?

An old request from 2014 Feedback Case #36887 where people don't understand why its useful, I guess it was overlooked again in the new edition.

@Norman P it is missing a number of convenienceuseful functions

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 ( https://docs.microsoft.com/en-us/dotnet/api/system.timespan.totalseconds?view=netframework-4.8 ), 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...

11. ### Ryan H

Feb 21 XDC Speakers

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)```

There is more than one method with this name but this does not match any of the available signatures

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

```Var d1 As DateTime = DateTime.FromString(TextField1.Text)
Var d2 As DateTime = DateTime.Now

var interval as DateInterval = d1 - d2

MessageBox interval.Days.ToString```

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?

12. ### Andre K

Feb 21 Pre-Release Testers

Xojo 2019r2.1

13. ### Ryan H

Feb 21 XDC Speakers

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

14. ### Andre K

Feb 21 Pre-Release Testers

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.

15. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

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

You sure youre not getting something also including part days (like hours min seconds etc as well)

16. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

@Andre K My license expired 23rd november just before r3.

Sympathies

17. ### Ryan H

Feb 21 XDC Speakers

@Norman P You sure youre not getting something also including part days (like hours min seconds etc as well)

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?

18. ### Julian S

Feb 21 Pre-Release Testers, Xojo Pro UK
Edited 7 weeks ago

@Ryan H 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.

.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.

19. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air
Edited 7 weeks ago

@Ryan H 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?

Nope
But there IS something up with the constructor
Gotta love optional params that arent optional :P (its a long standing behaviour of "optional" parameters see http://docs.xojo.com/Optional )

Youre providing an int, int, int, timezone and there is NO matching constructor :)
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)
```

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)
Feedback Case #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)
Feedback Case #59269

20. ### Norman P

Feb 21 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

@Ryan H 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?

Yes more or less