Crashed the party

Hi Cliff,

Have a look at http://documentation.xojo.com/api/language/extends.html.

Extends tells the IDE that I’m adding a method to an object, in this case a DateTime object called Value. Because I’m adding that method to every DateTime object in the project the method needs to have global scope and be in a module.

Another method is the one for determining if a DateTime object value is a leap year which you’ve seen before.

Public Function LeapYear(Extends Value As DateTime) as Boolean
  Var dt As New DateTime(Value.Year, 12, 31, 12, 0, 0, 0, TimeZone.Current)
  Return dt.DayOfYear = 366
End Function

Which returns true if the 31st of December in the year of the date at midday has a day of year number of 366. I chose midday so the calculation wouldn’t be affected by day light savings adjustments.

I have dozens of these methods in various modules and once you are proficient with the concept you’ll never stop using them.

1 Like

Okay this is actual confusing to me.

Var offTime As DateTime = DateTime.Now.ZeroHour.AddInterval(0, 0, 1, 2, 30)

How does this work? The result of ZeroHour is derived from a SubtractInterval whose values came from an AddInterval… What?

Basically, he’s creating a Date of Today at midnight and then adding 1 day, 2 hours and thirty minutes (or 2:30 AM tomorrow).

Value.SubtractInterval … I’m trying to figure out how it works.

It is subtracting the hours, minutes, seconds & nanoseconds from the DateTime object resulting in 12am on that day.

Like a time reset. 0’s everything out? 12am or 24 or either? Not sure if that’s correct.

It is the equivalent of

Var d As New Date
d.Hour = 0
d.Minute = 0
d.Second = 0

In the old API.

Okay, since I want to understand this. The current hour, min, second and nanosecond are passed to the function to be subtracted from the current DateTime.now which leaves the current year, month and day and everything else is now 0. Am I following this correctly?

Yes, Value is the DateTime, Value.Hour is the Hour part, Value.Minute the Minute and so on.

Good, if I don’t understand it I wouldn’t want to use it.

2 Likes

I wanted you to know I have now use this extension method and it works great. Do you have others that I might find useful or interesting to look at?

Hi Cliff,

Glad you worked it out. I’m currently in discussions with Dana to find a way to share these methods etc. which will make them easier to find.