DATETIME Data Type

Description

A variable of type DATETIME stores a date plus a time calculated to microseconds. The DATETIME data type represents values from 4713 B.C.E. (B.C.) to 19999 C.E. (A.D.).

Notes

You can use relational operators between two variables of type DATETIME. You can cast a variable of type DATETIME to or from a type DATE or a type TIME by means of explicit type casting. You can implicitly cast a STRING as a DATETIME as described in the table below.

You can generate a DATETIME constant by specifying a date and time as a string and then casting the string as a DATETIME data type. The string must have the following format:

YYYY-MM-DD HH:MM:SS.MSMSMS.

Rules Covering Type Casting

The following tables describes the rules for type casting:

Cast Rules
DATETIME to DATE The time information is lost. Use explicit type casting ([DATE])
DATETIME to TIME The date information is lost. Use explicit type casting ([TIME])
DATE to DATETIME The time is set to midnight. Use explicit type casting ([DATETIME])
TIME to DATETIME The date is set to the current date. Use explicit type casting ([DATETIME])
STRING to DATETIME

The string must have the ISO format:

YYYY-MM-DD HH:MM:SS.MSMSMS

You can truncate the string at any point, as long as the last field is complete. Use implicit type casting (DATETIME)

Example

The example below is artificially contrived to show the following:

  • The creation of a DateTime constant by implicitly casting a string as a DATETIME.

  • The conversion, by explicit casting, of the DATETIME to a DATE (this drops the time portion of the DATETIME).

  • The explicit casting of the DATE value to a DATETIME (so that it can be formatted) and the formatting of the DATETIME.

  • Printing the formatted DATETIME. Note that the time value changes to midnight. Rules govern the various ways in which you can convert dates and times using casting.

DATETIME GrandOpening = "2006-01-02 09:00"
DATE OpenDate = [DATE] GrandOpening
STRING sOpening 
sOpening = FormatDateTime ([DATETIME]OpenDate, "mmmm, d, yyyy hh:nn AM/PM") 
// Print: "Grand Opening on January 2, 2007 12:00 AM."
Print ("Grand Opening on {sOpening}.")