Skip to main content
Version: 1.0.2

Datetimes

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 an ISO-8601 format.

CREATE event SET time = "2022-07-03T07:18:52Z";

SurrealDB handles all datetimes with nanosecond precision.

CREATE event SET time = "2022-07-03T07:18:52.841147Z";

SurrealDB handles all timezones, and automatically converts and stores datetimes as a UTC date.

CREATE event SET time = "2022-07-03T07:18:52.841147+02:00";

Datetime comparison

datetimes can be compared with each other using the advanced SurrealDB operators.

SELECT * FROM "2022-07-03T07:18:52Z" > "2022-01-03T01:43:78Z";

Datetimes and Durations

Durations can be used to modify and alter datetimes.

CREATE event SET time = "2022-07-03T07:18:52Z" + 2w;

Multi-part durations can also be used to modify datetimes.

CREATE event SET time = "2022-07-03T07:18:52.841147Z" + 1h30m20s1350ms;

Duration units

Durations can be specified in any of the following units:

UnitDescription
nsNanoseconds
usMicroseconds, alternative: µs
msMilliseconds
sSeconds
mMinutes
hHours
dDays
wWeeks
yYears

Next steps

You've now seen how to store, modify, and handle dates and times in SurrealDB. For more advanced functionality, take a look at the time functions, which enable extracting, altering, rounding, and grouping datetimes into specific time intervals.