This is one of those that has to be simple but I dont see it.
When I run the code, with Dec 17 2014 as Date.Now, I get days = 27 which is clearly incorrect.
What am I not seeing?
TIA, Tony
[code] DIM days As Integer
DIM dte As Date = Date.Now // this is Dec 17, 2014
Dim d2 As New Date(2015, 01, 31, TimeZone.Current)
Dim interval As DateInterval
A date interval automatically breaks down the parts for you, but you are only looking at Days. I think you are probably missing that there is 1 Month and X Days in the interval…
However Dec 17, 2014 to Jan 01, 2015 is 14 days and then Jan 01, 2015 to Jan 31, 2015 it is 1 month. So 27 days still seem incorrect.
[quote=153220:@Tony Marino]This is one of those that has to be simple but I dont see it.
When I run the code, with Dec 17 2014 as Date.Now, I get days = 27 which is clearly incorrect.
What am I not seeing?
TIA, Tony
[code] DIM days As Integer
DIM dte As Date = Date.Now // this is Dec 17, 2014
Dim d2 As New Date(2015, 01, 31, TimeZone.Current)
Dim interval As DateInterval
interval = d2 - dte
days = interval.Days[/code][/quote]
See the local date on that PC and the date in Date objects “dte” and “d2” on that PC at line “interval = d2 - dte” in debugger.
I just ran this code (OS X, desktop project) and am getting what I would expect:
[code]
Using Xojo.Core
Dim dte As Date = Date.Now // this is Dec 17, 2014
Dim d2 As New Date(2015, 01, 31, TimeZone.Current)
Dim interval As DateInterval
interval = d2 - dte
Dim years As Integer = interval.Years // years = 0
Dim months As Integer = interval.Months // months = 1
Dim days As Integer = interval.Days // days = 13
Does the Interval object hold the absolute value of the interval ?
I mean, I suppose If I get a diff date like mar/2/2014 - fev/1/2014, I get an interval object of 1 month and 1 day, and the same for mar/2/2016 - fev/1/2016, but in running days the first is 29 days diff (feb ended 28th in 2014) and the second have a 30 days diff. Those are 2 different intervals measure in days for one month and one day. If I sum the second interval to a base date I must have a resulting date with one day more than if I did it with the first.
[quote=153417:@Rick Araujo]Does the Interval object hold the absolute value of the interval ?
I mean, I suppose If I get a diff date like mar/2/2014 - fev/1/2014, I get an interval object of 1 month and 1 day, and the same for mar/2/2016 - fev/1/2016, but in running days the first is 29 days diff (feb ended 28th in 2014) and the second have a 30 days diff. Those are 2 different intervals measure in days for one month and one day. If I sum the second interval to a base date I must have a resulting date with one day more than if I did it with the first.[/quote]
No, there isn’t really an “absolute value” to an interval. Right now you can only get back the broken up components, but it would seem sensible to have a way to get a date interval that lets you get it as days or hours or whatever units you specifically want it represented as.
Should be great if the DateInterval could hold and return a real interval diff (in Seconds?) too, and it could be used for purposes needing absolute time intervals for math like :
Dim interval As DateInterval = DueToDate - TodaysDate
MsgBox("Your payment is late "+str(interval.AbsSecs/86400)+" days")
BaseDate = BaseDate + Interval // Sets the base date to a date in the future + interval AbsSecs
[quote=153491:@Tim Hare]A little more testing (read: while creating a sample project for feedback) and it seems related to Date.Now. This code produces the correct results:
d1 = new Date(2014, 12, 18)
d2 = new Date(2015, 01, 31)
interval = d2 - d1