Skip to main content

STRFTIME

Return a formatted datetime value based on a specified format.

See also:
[SELECT] [ Date Modifiers ]


Syntax

SELECT STRFTIME(format, datetime_expression [, modifier, ...]) FROM <multipartIdentifier>;

Arguments

format
The desired format for the datetime value specified by the datetime_expression value. See the table of formats below.

datetime_expression
The date or time value to be formatted.

modifier
Zero or more modifiers to be applied to the datetime_expression. For more information about modifiers, see Date Modifiers

Formats

FormatDescription
%dday of the month: 01-31
%ffractional seconds: SS.SSS
%Hhour: 00-24
%jday of the year: 001-366
%JJulian day number
%mmonth: 01-12
%Mminute: 00-59
%sseconds since 1970-01-01
%Sseconds: 00-59
%wday of week 0-6 with Sunday==0
%Wweek of the year: 00-53
%Yyear: 0000-9999
%%escape a percent symbol: %

Return Value(s)

Returns a formatted string representing the desired datetime format.


Examples

Return a date in US format

SELECT name, strftime('%m/%d/%Y', creationTimestamp) as created_at
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';

Convert a datetime to an epoch timestamp

SELECT name, strftime('%s', creationTimestamp) as created_at
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';

Calculate the days between two dates

SELECT name, (strftime('%s','now')-strftime('%s', creationTimestamp))/86400 as days_since_creation
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';

For more information, see https://www.sqlite.org/lang_datefunc.html