SQLite unixepoch() Function
The SQLite unixepoch() function converts a time value specified by a time value and modifiers to a Unix timestamp, that is, the number of seconds from UTC 1970-01-01 00:00:00.
Syntax
Here is the syntax of the SQLite unixepoch() function:
unixepoch(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- Fall back to the beginning of the month intime_valuestart of year- Fall back to the beginning of the year intime_valuestart of day- Fall back to the beginning of the day intime_valueweekday N- Advancetime_valueββto weekdayNunixepoch- Unix timestamp for nowjulianday- The Julian calendar days for nowautolocaltime- The current timeutc- The utc time
The
NNNrepresents a number. Can be a positive or negative number. IfNNNis negative, it means subtraction.
Return value
The SQLite unixepoch() function returns a Unix timestamp corresponding to a specified time value. If no arguments are provided, the unixepoch() function returns the Unix timestamp corresponding to the current datetime.
Examples
Here are some examples to show usages of the SQLite unixepoch() function.
-
Use the SQLite
unixepoch()function to get the Unix timestamp of the current time:SELECT unixepoch(), unixepoch('now');unixepoch() unixepoch('now') ----------- ---------------- 1658823494 1658823494 -
Use the SQLite
unixepoch()function to convert2022-07-26 12:00:00to a Unix timestamp:SELECT unixepoch('2022-07-26 12:00:00'), unixepoch('2022-07-26 12:00:00', '-1 second');unixepoch('2022-07-26 12:00:00') unixepoch('2022-07-26 12:00:00', '-1 second') -------------------------------- --------------------------------------------- 1658836800 1658836799 -
Use the SQLite
unixepoch()function to get the Unix timestamp corresponding to the last day of the current year:SELECT unixepoch('now', 'start of year', '1 year', '-1 day');unixepoch('now', 'start of year', '1 year', '-1 day') ----------------------------------------------------- 1672444800