Getting confused by dates

Why does this code return 166?

Halfway through the year is 182.

tDate.year = 2014 tDate.day = 15 tDate.Month = 6 MsgBox(str(tDate.DayOfYear))

Thanks

Terry

166 days from 1.1.2015 is correct. Where do you get 182?

I meant 2014. Can’t edit.

I can’t count neither.

However, I substituted 30 into the day to obtain the 30 June 2014 and the code returned 153.

Set year, then month, then day and you will get 181 for 30th of June 2014.

But yes, you’ll get 153 if you set the day before the month. Don’t ask me why.

A little comparison:

Dim tDate As new Date tDate.year = 2014 tDate.day = 30 tDate.Month = 6 MsgBox(str(tDate.DayOfYear))

This returns 153

Dim tDate As New Date(2014, 6, 30) MsgBox(str(tDate.DayOfYear))

This returns 181

NOTE: This just crossed with your reply as I was typing.

Why is it relevant that you have to set the items in a specific order?

http://documentation.xojo.com/index.php/Date

But that should not be the case with the 15th of June.

If you set the day first then for the 30th of June Xojo sets it to the 2nd of June, therefore 153.

I would call this a bug as it is not an edge case with date not existing. It seems it uses 28 days per month by default and then “counts over” if no month is set: 30 = 28 -> 1 -> 2

Ahh, illumination. From today on it is February (I was still mentally in January). So if you set the day to 30 it becomes the “30th” of February = the 2nd of March. Then you set the month to June, so the 2nd of June.

So always set year, then month, then day, and you should be fine.

So not a bug, just improper use of date :wink:

There was quite a bit of discussion about how to count date differences here:
https://forum.xojo.com/15374-getting-the-date-difference-in-years-months-days

@Markus Winter I think it is definitely a bug, because when I enter dates (year, day, month) etc. in different order, I expect all the time the same result. So the getter of computed property could (ok, it’s gonna be time consuming) calculate the correct date based on set data, so all the time the same result should be returned.

If the user enters an invalid date like 30th of February it must not calculate further to 2nd of March but return an Exception or any other error. I know that date handling is a complex topic, but imho it is important for a language to have proper date/time handling.

If the user enters it, you will get an error. If you set it in code, it will auto adjust. Not a bug, but definitely a gotcha.

[quote=163792:@Tom Bass]I think it is definitely a bug, because when I enter dates (year, day, month) etc. in different order, I expect all the time the same result. So the getter of computed property could (ok, it’s gonna be time consuming) calculate the correct date based on set data, so all the time the same result should be returned.

If the user enters an invalid date like 30th of February it must not calculate further to 2nd of March but return an Exception or any other error. I know that date handling is a complex topic, but imho it is important for a language to have proper date/time handling.[/quote]

Agreed.

Thanks for everyones help

All the best

Terry