The SQLite STRFTIME() function returns the date formatted according to the format string specified as the first argument. It is modeled after the strftime() function from the standard C library but with some differences.
Syntax
STRFTIME(format, time-value, modifier, modifier, ...)
Parameters
format |
Required. Specify the format string to format the outputted date and time string. It can be one of the following:
Format | Description |
%d | Day of the month (1-31) |
%f | Seconds with fractional seconds (SS.SSS) |
%H | Hour on 24-hour clock (00-23) |
%j | Day of the year (001-366) |
%J | Julian day number (fractional) |
%m | Month (01-12) |
%M | Minute (00-59) |
%s | Seconds since 1970-01-01 |
%S | Seconds (00-59) |
%w | Weekday (0-6)
(0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday) |
%W | Week number in the year (00-53) |
%Y | Year (0000-9999) |
%% | % as a literal |
|
time-value |
Required. Specify a time-value. It can be one of the following:
time-value | Description |
YYYY-MM-DD | Date value formatted as 'YYYY-MM-DD' |
YYYY-MM-DD HH:MM | Date value formatted as 'YYYY-MM-DD HH:MM' |
YYYY-MM-DD HH:MM:SS | Date value formatted as 'YYYY-MM-DD HH:MM:SS' |
YYYY-MM-DD HH:MM:SS.SSS | Date value formatted as 'YYYY-MM-DD HH:MM:SS.SSS' |
YYYY-MM-DDTHH:MM | Date value formatted as 'YYYY-MM-DDTHH:MM' where T is a literal character separating the date and the time |
YYYY-MM-DDTHH:MM:SS | Date value formatted as 'YYYY-MM-DDTHH:MM:SS' where T is a literal character separating the date and the time |
YYYY-MM-DDTHH:MM:SS.SSS | Date value formatted as 'YYYY-MM-DDTHH:MM:SS.SSS' where T is a literal character separating the date and the time |
HH:MM | Date value formatted as 'HH:MM' |
HH:MM:SS | Date value formatted as 'HH:MM:SS' |
HH:MM:SS.SSS | Date value formatted as 'HH:MM:SS.SSS' |
now | now is a literal used to return the current date. |
DDDDDDDDDD | Julian date number |
|
modifier, modifier, ... |
Optional. Specify modifiers. Each modifier is a transformation that is applied to the time value to its left. Modifiers are applied from left to right and are cumulative. The available modifiers are as follows:
modifier | Description |
[+-]NNN days | Number of days added/subtracted to the date |
[+-]NNN hours | Number of hours added/subtracted to the date |
[+-]NNN minutes | Number of minutes added/subtracted to the date |
[+-]NNN.NNNN seconds | Number of seconds (and fractional seconds) added/subtracted to the date |
[+-]NNN months | Number of months added/subtracted to the date |
[+-]NNN years | Number of years added/subtracted to the date |
start of month | Shifting the date back to the start of the month |
start of year | Shifting the date back to the start of the year |
start of day | Shifting the date back to the start of the day |
weekday N | Moves the date forward to the next date where weekday number is N (0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday) |
unixepoch | Used with the DDDDDDDDDD format to interpret the date as UNIX Time (ie: number of seconds since 1970-01-01) |
julianday | Used with the DDDDDDDDDD format to force the time-value number to be interpreted as a julian-day number |
auto | Used with the DDDDDDDDDD format to interpret the time-value as either a julian day number or a unix timestamp, depending on its magnitude. |
localtime | Adjusts date to localtime, assuming the time-value was expressed in UTC |
utc | Adjusts date to utc, assuming the time-value was expressed in localtime |
|
Return Value
Returns the date formatted according to the specified format string.
Example: Current date and time
The SQLite 'now' time-value can be used to get the current date and time.
SELECT STRFTIME('%Y-%m-%d', 'now');
Result: '2022-04-12'
SELECT STRFTIME('%Y-%m-%d %H:%M', 'now');
Result: '2022-04-12 07:15'
Example: First day of the month
To get the first day of the month, 'start of month' modifier can be used. See the example below:
SELECT STRFTIME('%Y-%m-%d', '2018-08-18', 'start of month');
Result: '2018-08-01'
SELECT STRFTIME('%Y-%m-%d', 'now', 'start of month');
Result: '2022-04-01'
Example: Last day of the month
To get the last day of the month, first, the 'start of month' modifier is used to calculate the start day of the month and then 1 month is added and then 1 day is subtracted.
SELECT STRFTIME('%Y-%m-%d', '2018-08-18', 'start of month', '+1 month', '-1 day');
Result: '2018-08-31'
SELECT STRFTIME('%Y-%m-%d', 'now', 'start of month', '+1 month', '-1 day');
Result: '2022-04-30'
Example: Adding/Subtracting years
To add/subtract years from a time-value, [+-]NNN years modifier can be used. In the example below 2 years is added and subtracted from a given time-value.
SELECT STRFTIME('%Y-%m-%d', '2018-08-18', '+2 years');
Result: '2020-08-18'
SELECT STRFTIME('%Y-%m-%d', '2018-08-18', '-2 years');
Result: '2016-08-18'
Example: Adding/Subtracting days
To add/subtract days drom a time-value, [+-]NNN days modifier can be used. In the example below 10 days is added and subtracted from a given time-value.
SELECT STRFTIME('%Y-%m-%d', '2018-08-18', '+10 days');
Result: '2018-08-28'
SELECT STRFTIME('%Y-%m-%d', '2018-08-18', '-10 days');
Result: '2018-08-08'
❮ SQLite Functions