A Date object stores the number of seconds since 12:00 AM, January 1, 1904. Properties of a Date enable you to get and set a day value only, a date/time, or only a time.
Also refer to the Date-related classes available in Xojo.Core:
|NOTE: 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:
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:
On Windows, the Regional and Language Options panel determines how dates are formatted. On OS X, the date formats are specified in the Languages & Region system preference 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.
This example creates a Date object and sets it to 15 April, 2012.
This example creates a Date and displays the current date in a message box.
This example sets a Date object to 10 February 1954.
The following example compares a specific date to the current 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!")
If d > today Then
MsgBox("That's after today!")
If d = today Then