DateTime

From Xojo Documentation

Class (inherits from Object)


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.

Properties
Day fa-lock-32.png Minute fa-lock-32.png Second fa-lock-32.png
DayOfWeek fa-lock-32.png Month fa-lock-32.png SecondsFrom1970 fa-lock-32.png
DayOfYear fa-lock-32.png Nanosecond fa-lock-32.png TimeZone fa-lock-32.png
Hour fa-lock-32.png SQLDate fa-lock-32.png WeekOfYear fa-lock-32.png
IsDaylightSavingsTime fa-lock-32.png SQLDateTime fa-lock-32.png Year fa-lock-32.png
Methods
AddInterval SubtractInterval ToString
Shared Methods
FromString Now
Constructors

Constructor(CopyDate as DateTime)


Constructor(Year as Integer, Month as Integer, Day as Integer, hour as Integer = 0, minute as Integer = 0, second as Integer = 0, nanosecond as Integer = 0, timeZone as TimeZone = Nil)


Constructor(secondsFrom1970 as Double, timeZone as TimeZone = Nil)


Enumerations
FormatStyles

Notes

fa-info-circle-32.png
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:

Var v As Variant
Var d As DateTime
d = DateTime.Now
v = d

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

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:

Var d As DateTime = DateTime.Now
MessageBox(d.ToString)

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.

Operators

  • 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.

Sample Code

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

Var d As New DateTime(2012, 4, 15)

This code displays the current date in a message box.

Var d As DateTime = DateTime.Now
MessageBox(d.ToString(Locale.Current))

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

Var d As New DateTime(2012, 12, 5)
Var today As DateTime = DateTime.Now

If d < DateTime.Now Then
MessageBox("That's before today!")
End If

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

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

See Also

Microseconds, Ticks functions; FolderItem, DateInterval, TimeZone classes