From Xojo Documentation
|You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!|
New in 2019r2
A DateTime object stores the number of seconds since 12:00 AM, January 1, 1970, i.e., "1970-01-01 00:00:00". Properties of a DateTime enable you to get and set a day value only, a date/time, or only a time.
|The oldest date DateTime can accept is 0001-01-01 00:00.|
To create a DateTime object, you must set date/time you want. You can do this via DateTime.Now or by passing date/time information to one of the constructors. If you're going to change a date, use DateTime.AddInterval, the + operator, DateTime.SubtractInterval or the - operator as these will handle things like leap years, time zone differences and more.
Because DateTime implements Operator_Compare, you can use the normal comparison operators to compare DateTime values.
If you pass invalid values (such as 32 for the day, 13 for the month) when creating a DateTime, an InvalidArgumentException 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:
When using DateTime.ToString with DateTime.FormatStyles to get a formatted date or time, the actual format in which the date or time will appear is affected by the user's operating system settings. The user’s system settings control the ultimate formats that are used.
You can use the DateTime.ToString function to obtain the string value of the date in default system 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 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.
- You can use the + operator to add a DateTime and an Interval together to get a new DateTime.
- You can use the - operator to subtract a DateInterval from a DateTime to get a new DateTime.
- You can use the - operator to subtract a DateTime from a DateTime to get a new DateInterval.
This code creates a DateTime object and sets it to 15 April, 2012.
This code displays the current date in a message box.
The following code compares a specific date to the current date:
Var today As DateTime = DateTime.Now
If d < DateTime.Now Then
MessageBox(d.ToString + " is earlier than now!")
If d > today Then
MessageBox(d.ToString + " is later than now!")
If d.Year = today.Year And d.Month = today.Month _
And d.Day = today.Day Then
MessageBox("The dates match.")