Skip to content

Let’s talk DateTime

In Xojo Release 2019R2 a new DateTime class was introduced.  This is a Time zone aware Date Class and is immutable which is great compared to the deprecated Date class.  How many of us have had issues working out why when we stepped through changing a Date it worked, but didn’t when not?  I know I did in the early days.

DateTime is cool. You can create a variable that stores the date/time now with:

Var dt As DateTime = DateTime.Now


Var dt As New DateTime(TimeZone.Current)

The second variant allows you to simply find the date/time now in any time zone:

Var dt As New DateTime(New TimeZone("NZ")) ' Local time for me and Rocket Lab launch site 1


Var dt As New DateTime(New TimeZone("UTC")) ' Local time in Greenwich

Sometimes you need to calculate a date:

Var dt As DateTime = DateTime.Now.AddInterval(0, 1) ' today next month


Var dt As DateTime = DateTime.Now.SubtractInterval(0, 1) ' today last month

You can obviously use the components of a DateTime in your calculation, e.g. finding the 20th of next month:

Var dt As DateTime = DateTime.Now.AddInterval(0, 1, 20 – DateTime.Now.Day)

DateTime is based on seconds from 1970 which allows for simple conversions between TimeZones and is daylight savings time aware.

How awesome is this?

Wayne Golding has been a Xojo developer since 2005. He operates the IT Company Axis Direct Ltd which primarily develops applications using Xojo that integrate with Xero Wayne’s hobby is robotics where he uses Xojo to build applications for his Raspberry Pi, often implementing IoT for remote control.