Datetimes
SurrealDB has native support for datetimes with nanosecond precision. SurrealDB automatically parses and understands datetimes which are written as strings in the SurrealQL language. Times must also be formatted in RFC 3339 format.
Datetimes are represented by and can be created using a d prefix in front of a string.
SurrealDB handles all datetimes with nanosecond precision.
A datetime can be created with a timezone, which will be converted and stored as a UTC date.
A datetime can also be created by using <datetime> to cast from a string.
With correct input:
With incorrect input (missing final Z):
As a convenience, a date containing a year, month and day but no time will also parse correctly as a datetime.
Datetime types in DEFINE FIELD statements
Defining a field with a set datetime type will ensure that datetimes are properly formatted and not passed on as simple strings.
The above query will fail because the datetime is not cast as a datetime type. The correct input is:
Datetime comparison
A datetime can be compared with another using SurrealDB operators.
Durations and datetimes
A duration can be used to alter a datetime.
Multi-part durations can also be used to modify datetimes.
Altering datetimes
Each value in a datetime can be set by using one of seven time::set_ functions. Each of these function names ends with the part of the datetime that is modified, such as time::set_year() or time::set_hour().
As these functions do not modify an existing datetime but return a new one, they can be chained one after another.
See also
time functions, which enable extracting, altering, rounding, and grouping datetimes into specific time intervals