[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)
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
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.
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.
[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
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
// then you can ask the delta time how many days are between the two dates
[/code]
It’s all free for the taking