SQLite time() Function
The SQLite time() function converts a time value specified by a time value and modifiers to a time string in HH:MM:SS format.
Syntax
Here is the syntax of the SQLite time() function:
time(time_value [, modifier, modifier, ...])
Parameters
time_value-
Optional. A time value. The time value can be in any of the following formats, as shown below. The value is usually a string, but in the case of format 12 it can be an integer or a floating point number.
YYYY-MM-DDYYYY-MM-DD HH:MMYYYY-MM-DD HH:MM:SSYYYY-MM-DD HH:MM:SS.SSSYYYY-MM-DDTHH:MMYYYY-MM-DDTHH:MM:SSYYYY-MM-DDTHH:MM:SS.SSSHH:MMHH:MM:SSHH:MM:SS.SSSnow- the current date and timeDDDDDDDDDD.dddddd- Julian days number with fractional part
modifier-
Optional. You can use zero or more modifiers to change the time value
time_value. Multiple modifiers are applied sequentially from left to right. You can use modifiers like this:NNN days- AddNNNdays to the time valueNNN hours- AddNNNhours to the time valueNNN minutes- AddNNNminutes to the time valueNNN.NNNN seconds- AddNNN.NNNNseconds to the time valueNNN months- AddNNNmonths to the time valueNNN years- AddNNNyears to the time valuestart of month- Move the time value to the start of its month.start of year- Move the time value to the start of its year.start of day- Move the time value to the start of its day.weekday N- Advance to weekdayN(Sunday is0); if already on that day, leave the date unchanged.unixepoch- Interpret the immediately preceding numeric time value as Unix seconds.julianday- Interpret the immediately preceding numeric time value as a Julian day number.auto- Interpret a numeric time value as a Julian day number or Unix timestamp based on its magnitude; Unix timestamps in the first 63 days of 1970 are ambiguous.localtime- Assume the preceding time value is UTC and convert it to local time.utc- Assume the preceding time value is local time and convert it to UTC.subsecorsubsecond(SQLite 3.42.0+) - Return fractional seconds fromtime(),datetime(),unixepoch(), andstrftime('%s'); no effect ondate()orjulianday().ceilingandfloor(SQLite 3.46.0+) - Resolve ambiguous month/year shifts;ceilingis the default, whilefloorchooses the last day of the previous month.
The
NNNrepresents a number. Can be a positive or negative number. IfNNNis negative, it means subtraction.
The unixepoch, julianday, and auto modifiers must immediately follow the initial numeric time value. See the SQLite date and time function documentation for ordering rules and edge cases.
Return value
The SQLite time() function returns a time string in HH:MM:SS format. If no arguments are provided, the time() function returns the current time.
Examples
Here are examples that show how to use the SQLite time() function. When the time-value is omitted or set to 'now', SQLite uses the current UTC time, so the exact result changes over time.
-
Get the current time using the SQLite
time()function:SELECT time();Alternatively, you can use the SQLite
time()function with a time value'now'to get the current time:SELECT time('now'); -
Use the SQLite
time()function to get the time part of a datetime value:SELECT time('2022-07-26 12:01:01');time('2022-07-26 12:01:01') --------------------------- 12:01:01 -
Add 10 minutes to a fixed time value:
SELECT time('2022-07-26 08:03:02') AS original, time('2022-07-26 08:03:02', '10 minutes') AS ten_minutes_later;original ten_minutes_later -------- ----------------- 08:03:02 08:13:02