The Xojo.Core.Date Class

Published December 11, 2014 by
Paul Lefebvre

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: