Date

From Xojo Documentation

Class (inherits from Object)

A Date object stores the number of seconds since 12:00 AM, January 1, 1904, i.e., "1904-01-01 00:00:00". Properties of a Date enable you to get and set a day value only, a date/time, or only a time.

Properties
AbbreviatedDate fa-lock-32.png LongDate fa-lock-32.png Second
Day LongTime fa-lock-32.png ShortDate fa-lock-32.png
DayOfWeek fa-lock-32.png Minute ShortTime fa-lock-32.png
DayOfYear fa-lock-32.png Month TotalSeconds
GMTOffset SQLDate WeekOfYear fa-lock-32.png
Hour SQLDateTime Year
Constructors

Constructor()


Constructor(CopyDate as Date)


Constructor(Year as Integer, Month as Integer = 1, Day as Integer = 1, hour as Integer = 0, minute as Integer = 0, second as Integer = 0)


Constructor(Year as Integer, Month as Integer = 1, Day as Integer = 1, hour as Integer = 0, minute as Integer = 0, second as Integer = 0, GMTOffset as double)


Notes

fa-info-circle-32.png
On Windows (due to the Windows API being used), it is not possible to go back further than 1 January 1601.

When you create and instantiate a Date object, it is initialized to the current date and time. Therefore the order of assignment is important: when dealing with a day that exists in one month when the current system date is in another (such as December 31 when you're in November), setting the Day before the month can result in an unexpected (here 12/01/2011) or even invalid date. Not setting the year before entering Feb 29 will cause oddities if the current year is not a leap year.

Because Date implements Operator_Compare, you can use the normal comparison operators to compare date values.

If you try to set the date or date/time and the format is incorrect, an UnsupportedFormatException is raised.

The date properties of FolderItems can be accessed via the CreationDate and ModificationDate properties of FolderItem objects. You can get the current date and time by creating a new date and reading the values of the Year, Month, Day, Hour, Minute, and Second properties.

In the following code:

Dim v As Variant
Dim d As Date
d = New Date
v = d

What is actually happening is that the Variant stores the value of the TotalSeconds property as a Double, along with the type information that it is a Date (the Variant's Type property = 7).

Although Date is a class that is subclassed from Object, VarType identifies a Date as a Date data type (Type=7) rather than an Object (Type=9).

Use the ParseDate function to convert a date string to a Date value.

The TotalSeconds property is the “master” property that stores the date/time associated with a Date. The other property values are derived from TotalSeconds. If you change the value of the TotalSeconds property, the values of the Year, Month, Day, Hour, Minute, and Second properties change to reflect the second on which TotalSeconds occurs. Conversely, if you change any of these properties, the value of TotalSeconds changes commensurately.

The Date properties that return formatted date or time information are affected by the user's operating system settings. The user’s system settings control the formats that are used.

You can use the Str function to obtain the string value of the date in SQL date/time format, i.e., the following gets the string value of the current date/time:

Dim d As New Date
MsgBox(Str(d))

On Windows, the Regional and Language Options panel determines how dates are formatted. On macOS, the date formats are specified in the Languages & Region System Preferences panel.

If you need to control the exact appearance of date/time information, the best way is to extract the information yourself and manage the formatting using string manipulation functions.

The ParseDate function accepts a date as a string and converts it to a Date object. It accepts only date strings that are in any of the formats specified by the user's system settings.

Sample Code

This code creates a Date object and sets it to 15 April, 2012.

Dim d As New Date(2012, 4, 15)

This code creates a Date and displays the current date in a message box.

Dim d As New Date
MsgBox(d.ShortDate)

This code sets a Date object to 10 February 1954.

Dim d As New Date
d.Year = 1954
d.Month = 2
d.Day = 10
MsgBox(d.ShortDate)

The following code compares a specific date to the current date:

Dim d As New Date
d.Year = 2012
d.Month = 12
d.Day = 5

Dim today As New Date // Newly created dates default to the current date and time

If d < today Then
MsgBox("That's before today!")
End If

If d > today Then
MsgBox("That's after today!")
End If

If d = today Then
MsgBox("That's today!")
End If

See Also

Microseconds, ParseDate, Str, Ticks functions; FolderItem, Xojo.Core.Date, Xojo.Core.DateInterval, Xojo.Core.TimeZone classes