getting days between 2 dates

Hi there…
Has anybody got a code snippet that will return the distance in days between 2 dates?

My software has a 10 day trial period and I want the splash page to let the user know how may days until expiry

I didn’t see this yet in the forum so it might be helpful for other people in the same boat :slight_smile:
Cheers

dim first as new date(2013,1,1)
dim second as new date(2013,1,10)
dim days as integer
days = second - first

[quote=39676:@Brian O’Brien]dim first as new date(2013,1,1)
dim second as new date(2013,1,10)
dim days as integer
days = second - first[/quote]
Not quite. You can’t just subtract one date from another but you can use the TotalSeconds property:

dim first as new date(2013,1,1) dim second as new date(2013,1,10) dim days as integer days = (second.totalseconds - first.TotalSeconds)/86400 MsgBox str(days)

Thanks guys - that last examples was exactly what I was looking for!
Works flawlessly!

Beware though. If you use a SQLdate to set one of the dates, , Xojo has the disturbing habit of using the current Hours, minutes and seconds. You end up having to Zero then out.

dim d as new date d.SQLDate="2014-09-13" MsgBox d.SQLDateTime

BUG: 23432 - SQLDATE should set hour, minute second to 0, SQLDateTime should use the current time

[quote=39687:@Jay Menna]Beware though. If you use a SQLdate to set one of the dates, , Xojo has the disturbing habit of using the current Hours, minutes and seconds. You end up having to Zero then out.

dim d as new date d.SQLDate="2014-09-13" MsgBox d.SQLDateTime

BUG: 23432 - SQLDATE should set hour, minute second to 0, SQLDateTime should use the current time[/quote]

Not really a bug just not behaviour you’re expecting
If you want the date & time set then use SQLDateTime

dim d as new date
d.SQLDateTime="2014-09-13 00:00:00"
MsgBox d.SQLDateTime

Changing this behavior at this point runs the risk of breaking a lot of code silently

Actually, I get one of the dates from a database
It’s working well

dim d as new date
d = rs.Field(“Started”).DateValue

works without any adverse affects
Sean

I once put a lot of work in DateDifferenceMBS class.
It calculates the difference between two dates with given the delta in years, months, days or just total days.

Norman: I agree with you and disagree with you.

Yea, it is not what I expected. But the fine art of UI is to give me what I expected. I did a highly unscientific survey of 2 other programmers and they expected it to be 0:0:0 also.

Im not throwing rocks here. But looking at the newbee threads on Dates back in the Realbasic and Realstudio lists. It appears that almost everyone scratches their head when they first encounter the date class. This predates you at real/xojo and i suspect it was a design decision made my Andrew when it was Cross basic.

That’s correct. The Date class was in v1.0.

[quote=40155:@Jay Menna]
This predates you at real/xojo and i suspect it was a design decision made my Andrew when it was Cross basic.[/quote]
I’ve been using it since then - worked here only the last 5 years.
The hard part with things that have been like this “since the beginning of time” is theres virtually no decent way for us to warn people that some behavior has changed & they should go check their code.
I certainly would not be surprised to find that somewhere, someone IS depending on the date class behaving this way and if we change it we very silently break their project.
So I’d do as I suggested & set the SQLDateTime so you CAN be certain that date & time are as you expect.
And we dont have to go break someone else project.

IF I were designing this from scratch I’d probably make the time be 00:00:00 - but that decision was made long ago :frowning:
However, thats probably something we should change for the new framework as that does present an opportunity to correct these little oddities

And for anyone still watching there is a DeltaTime class and module on my site that I wrote many years ago
http://great-white-software.com/deltaTime.zip
It lets you add & subtract delta time values to dates & get whatever the resulting date is
And there’s an extensions method in there that lets you do

[code]dim d1 as new date(2013, 10, 07)
dim d2 as new date(2012, 10, 07)
dim delta as DeltaTime = d1.minus(d2) // sadly I cant make this be d1 - d2 :frowning:

// then you can ask the delta time how many days are between the two dates
[/code]
It’s all free for the taking