Date and Time Functions
Turso provides a complete set of date and time functions compatible with SQLite. These functions accept time values in various formats, apply optional modifiers, and return results as TEXT, REAL, or INTEGER depending on the function. All date and time functions use the proleptic Gregorian calendar and assume UTC unless thelocaltime modifier is applied.
Functions
date()
Returns the date as TEXT inYYYY-MM-DD format.
Returns: TEXT in
YYYY-MM-DD format, or NULL if any argument is invalid.
time()
Returns the time as TEXT inHH:MM:SS format.
Returns: TEXT in
HH:MM:SS format, or NULL if any argument is invalid.
datetime()
Returns the date and time as TEXT inYYYY-MM-DD HH:MM:SS format.
Returns: TEXT in
YYYY-MM-DD HH:MM:SS format, or NULL if any argument is invalid.
julianday()
Returns the Julian day number as a REAL. The Julian day number is the number of days since noon on November 24, 4714 B.C. (proleptic Gregorian calendar).
Returns: REAL representing the Julian day number, or NULL if any argument is invalid.
unixepoch()
Returns the Unix timestamp as an INTEGER. The Unix timestamp is the number of seconds since1970-01-01 00:00:00 UTC.
Returns: INTEGER representing seconds since the Unix epoch, or NULL if any argument is invalid. If the
subsec modifier is used, returns REAL with fractional seconds.
strftime()
Returns a formatted date/time string according to the specified format string.
Returns: TEXT with format codes replaced by date/time components, or NULL if any argument is invalid.
The other date/time functions can be expressed as
strftime calls:
strftime Format Codes
timediff()
Returns the difference between two time values as a TEXT string in the format(+|-)YYYY-MM-DD HH:MM:SS.SSS. The result represents the time that must be added to time2 to produce time1.
Returns: TEXT in
(+|-)YYYY-MM-DD HH:MM:SS.SSS format, or NULL if either argument is invalid.
Time Value Formats
All date and time functions accept time values in the following formats.When passing a Unix timestamp as the time value, you must include the
'unixepoch' modifier so the function knows to interpret the number as seconds since 1970-01-01, not as a Julian day number.Modifiers
Modifiers transform the time value. Multiple modifiers are applied left to right. If any modifier is invalid, the function returns NULL.Offset Modifiers
Offset modifiers add or subtract a specified amount from the time value.
NNN can be a positive or negative integer (or real number for seconds). The
+ sign is optional for positive values.
Time Offset Modifier
A time offset in the format+HH:MM or -HH:MM adds or subtracts the specified hours and minutes.
Start-of Modifiers
These modifiers reset the time value to the start of a period.Weekday Modifier
Theweekday N modifier advances the date to the next occurrence of the specified weekday, where 0 = Sunday, 1 = Monday, …, 6 = Saturday. If the current date already falls on that weekday, it is unchanged.
Interpretation Modifiers
Timezone Modifiers
Rounding Modifiers
These modifiers affect how month arithmetic handles months with different numbers of days.
Subsec Modifier
Thesubsec modifier causes unixepoch() to return a REAL with fractional seconds instead of truncating to INTEGER.
Practical Examples
Get the Current Date and Time
Date Arithmetic
Age Calculation
Convert Between Formats
Group Records by Time Period
Filter by Date Range
Compute Elapsed Time
See Also
- Expressions for using functions in expressions
- Data Types for TEXT, REAL, and INTEGER storage classes
- Scalar Functions for other built-in functions