Some days ago (or long, long ago, depending when do you read these lines) I received an email from a developer that was porting code from his old VisualBasic domain to the native, multi-platform Xojo. He asked me how can to get the difference between two dates? I’m pretty sure that most of you will have the answer, but I told him he’ll need Xojo.Core.Date and Xojo.Core.DateInterval. If you want to know how easy it is or how to get the same result for all your code based on the old date class, then I invite you to continue reading…
Use the new Framework, Luke
The Xojo.Core.Date and Xojo.Core.DateInterval classes under the new, Xojo framework make it really easy to get the difference between two given dates. The code you’ll need for this is as easy as this:
Using Xojo.Core Dim dateA As date = New Date(2017,10,23, TimeZone.current) Dim dateB As date = New Date(2017,11,23, TimeZone.Current) Dim result As DateInterval = dateB - dateA MsgBox result.Years.ToText+ " " + result.Months.ToText + " " + result.Days.ToText
The key here is in the use of the subtract operator that gives as result a TimeInterval instance. Thus, accessing the properties of this instance will get the years, months and days elapsed between the two compared dates. Take into account the importance of the order of your Date operands when used with the subtract operator. I mean, if the rightmost date is higher than the leftmost date, then probably you will get some negative values for the properties. In some cases, this will be the expected behaviour for the logic of your app… other times, you will prefer to check this before getting the DateInterval instance in order to get proper values.
Date difference for existing code
But, what if you want to get the same function for your current code based on the date class? Here you can opt for several options. The one we are going to put in practice is based in the use of the Xojo Module in combination with the Class Extension OOP feature. In fact, Class Extension is the way we can extend the functionallity of an existing Class. Straight, right? So, in order to achieve this the first step, add a new Module to a Xojo project and name it using the Inspector Panel (for example, “Extents”). Then, select the newly created module and add a new Method to it using the following signature:
- Method Name:
Difference
- Parameters:
Extends d As Date, RightDate As Date
- Return Type:
Dictionary
Next, we will add some text constants to the Module, that will be used by the Method and help any calling code that wants to access the result.
- kDay As String = Day
- kDay As String = Month
- kYear As String = Year
Afterwards, select the Difference
method and type the following lines of code in the resulting Code Editor:
Dim difference As Double = RightDate.TotalSeconds - d.TotalSeconds Dim days As Integer Dim years As Integer Dim months As Integer Dim dateResult As New date dateResult.TotalSeconds = difference days = dateResult.Day - 1 years = dateResult.Year - 1904 months = dateResult.Month - 1 Dim result As New Dictionary result.Value(kDay) = days result.Value(kYear) = years result.Value(kMonth) = months Return result
As you can see, the code is simple enough. In order to simplify the code to a maximum, this example opts to give the result as an Instance of Dictionary and using the previously definied constant to store the “Year”, “Months” and “Days” of difference between the compared dates. If you want, you can use an additional Structure or Class to return the result (your own DateInterval?).
To test the new Class Extension, add an Open Event to the Window or App object of your project and type this lines of code:
Dim d As New date(2017,10,24) Dim d1 As New date(2017,11,30) Dim interval As Dictionary = d.Difference(d1) MsgBox interval.Value(kYear) + " " + interval.Value(kMonth) + " " + interval.value(kDay)
A new Date Class that makes the Difference
There is a third option that we can explore that is more OOP oriented: create our own Date class and implement the subtract operator on it. More or less the thing that we do when using Xojo.Core.Date.
For that, the first step is adding a new class to a Xojo Project (Insert > Class). Use the following data in the Inspector Panel:
- Name: MyDateClass
- Super: Date
Next, add to the class the same constant we defined in the previous section (kDay, kMonth and kYear). These will be used to define the keys in the resulting dictionary and to ease operations from the calling code too.
The most important thing is adding a new Method to our class with the following data:
- Method Name: Operator_Subtract
- Parameters: rightMostDate as Date
- Return Type: Dictionary
The Operator_Subtract method instructs the Xojo compiler so you can use the minus sign (-) between two instances of the class. Type these lines of code for the method:
Dim difference As Double = self.TotalSeconds - rightMostDate.TotalSeconds Dim days As Integer Dim years As Integer Dim months As Integer Dim dateResult As New date dateResult.TotalSeconds = difference days = dateResult.Day - 1 years = dateResult.Year - 1904 months = dateResult.Month - 1 Dim result As New Dictionary result.Value(kDay) = days result.Value(kYear) = years result.Value(kMonth) = months Return result
Then in the Open Event of a Window, or the App object, check the class with the following code:
Dim d2 As New MyDateClass(2017,10,24) Dim d3 As New MyDateClass(2017,11,30) interval = d3 - d2 MsgBox interval.Value(kYear) + " " + interval.Value(kMonth) + " " + interval.value(kDay)
Javier Rodriguez has been the Xojo Spanish Evangelist since 2008, he’s also a Developer, Consultant and Trainer who has be using Xojo since 1998. He manages AprendeXojo.com and is the developer behind the GuancheMOS plug-in for Xojo Developers and the Snippery app, among others.