Ready to learn a bit about the Date class in the new Xojo framework?
First, remember the old Date class remains available for desktop/web/console projects.
In this post I’m specifically talking abou the new Xojo.Core.Date class. The big change is that Xojo.Core.Date is now immutable. With the old Date class, if you changed Date properties in the “wrong” order, such as set Day to 31 before setting Month to 9 then unexpected things start to happen. It also caused problems with things like FolderItem.ModificationDate (for example) because you might think you could actually change the modification date by directly changing its Date properties, but that doesn’t actually work.
So Xojo.Core.Date is immutable. You get a new Date by using one of the Constructors. If you need the current date, you use Date.Now. And with this we’ve added the companion DateInterval and TimeZone classes to make it easy to do math on Dates and to convert a date/time to a different time zone.
In particular, you can now do arithmetic on Dates. For example, if you want to get the Date that is two months earlier than now, you can create a DateInterval and subtract it from the Date:
Dim twoMonths As New DateInterval twoMonths.Month = 2 // 2 month interval // Get date two months before today Dim past As Date = Date.Now - twoMonths
Or if you want to know the the interval until Jan 1, 2030:
Dim d2 As New Date(2030, 1, 1, TimeZone.Current) Dim interval As DateIntervalinterval = d2 - Date.Now
Need to convert the current date/time to GMT? Try this:
Dim GMTZone As New TimeZone("GMT") Dim GMTDate As New Date(Date.Now.SecondsFrom1970, GMTZone)
You can use Xojo.Core.Date in all project types (Desktop, Web, Console and iOS).
Update (June 2020):
Since this was written, the DateTime class was added as part of API 2.0.