Date interval confusion

This is one of those that has to be simple but I don’t 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]

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…

Do not have access to v2014r3, so cannot confirm.

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 don’t 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.

You can just download it

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

Break[/code]

No hard disk space for the trial v2014r3 on this (read only) PC.

AH well THAT is a different issue :stuck_out_tongue:

That’s weird. I get really bogus values on Windows with Paul’s code. Is there a bug report already?

days = 0
months = 2
years = 2015

Please file one if you see an issue on a supported version of Windows.

Classic case of looking but not seeing.

Thanks Travis

[quote=153265:@Tim Hare]That’s weird. I get really bogus values on Windows with Paul’s code. Is there a bug report already?

days = 0
months = 2
years = 2015[/quote]

Same here on Windows 8.1

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

If you really just want absolute days, you can do that without intervals, as Dates already have an absolute seconds property.

DaysLate = (DueToDate.SecondsFrom1970 - TodaysDate.SecondsFrom1970) / 86400

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

But this code does not

d1 = Date.Now
d2 = new Date(2015, 01, 31)
interval = d2 - d1

I will test it again in r3.1

Same results on 2014r3.1

<https://xojo.com/issue/37418>

I see that Travis had already entered a case this morning. My feedback search skills are apparently lacking.

<https://xojo.com/issue/37413>

Ha, but thanks for the additional info you provided. It’s helpful. :slight_smile:

[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

But this code does not

d1 = Date.Now
d2 = new Date(2015, 01, 31)
interval = d2 - d1

I will test it again in r3.1[/quote]

The first code also gives wrong results: 2 months and 13 days, this should be 1 months and 13 days. Difference of 1 month.

The second code gives also wrong results as you mentioned before.