TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

SQLite strftime

SQLite strftime with history, features, advantages, installation, commands, syntax, datatypes, operators, expressions, databases, table, crud operations, clauses, like, glob, limit, and clause, advance sqlite

<< Back to SQLITE

SQLite strftime Function

The SQLite strftime function is a very powerful function which facilitates you to fetch date and time and also perform date calculation.

Syntax:

strftime(format, timestring [, modifier1, modifier2, ... modifier_n ] ) 

Here, format can be any of the following:

Index format Explanation
1) %Y Year as 4 digits (0000 to 9999)
2) %W Week of year (00 to 53)
3) %w Day of week (0 to 6, where 0 is Sunday)
4) %m Month of year (01 to 12)
5) %d Day of month (00 to 31)
6) %H Hour (00 to 24)
7) %M Minute (00 to 25)
8) %S Seconds (00 to 59)
9) %s Seconds since 1970-01-01
10) %f Fractional seconds (SS.SSS)
11) %j Day of year (001 to 366)
12) %J Julian day as a numeric value

Here, timestring is a date value which can be anyone of the following:

Index timestring Description
1) now It is a literal used to return the current date.
2) YYYY-MM-DD It specifies the date value formatted as 'YYYY-MM-DD'
3) YYYY-MM-DD HH:MM It specifies the date value formatted as 'YYYY-MM-DD HH:MM'
4) YYYY-MM-DD HH:MM:SS It specifies the date value formatted as 'YYYY-MM-DD HH:MM:SS'
5) YYYY-MM-DD HH:MM:SS.SSS It specifies the date value formatted as 'YYYY-MM-DD HH:MM:SS.SSS'
6) HH:MM It specifies the date value formatted as 'HH:MM'
7) HH:MM:SS It specifies the date value formatted as 'HH:MM:SS'
8) HH:MM:SS.SSS It specifies the date value formatted as 'HH:MM:SS.SSS'
9) YYYY-MM-DDTHH:MM It specifies the date value formatted as 'YYYY-MM-DDTHH:MM' where t is a literal character separating the date and time portions.
10) YYYY-MM-DDTHH:MM:SS It specifies the date value formatted as 'YYYY-MM-DDTHH:MM:SS' where t is a literal character separating the date and time portions
11) YYYY-MM-DDTHH:MM:SS.SSS It specifies the date value formatted as 'YYYY-MM-DDTHH:MM:SS.SSS' where t is a literal character separating the date and time portions
12) DDDDDDDDDD It specifies the Julian date number

modifier1, modifier2, ... modifier_n: modifiers are optional. These are used with timestring to add or subtract time, date or years.

Index Modifier Description
1) [+-]NNN years It is used to specify number of years added/subtracted to the date
2) [+-]NNN months It is used to specify number of months added/subtracted to the date
3) [+-]NNN days It is used to specify number of days added/subtracted to the date
4) [+-]NNN hours It is used to specify number of hours added/subtracted to the date
5) [+-]NNN minutes It is used to specify number of minutes added/subtracted to the date
6) [+-]NNN seconds It is used to specify number of seconds added/subtracted to the date
7) [+-]NNN.NNNN seconds It is used to specify number of seconds (and fractional seconds) added/subtracted to the date
8) start of year It is used to shift the date back to the start of the year
9) start of month It is used to shift the date back to the start of the month
10) start of day It is used to shift the date back to the start of the day
11) weekday N It is used to move 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)
12) unixepoch It is used with the DDDDDDDDDD timestring to interpret the date as UNIX Time (ie: number of seconds since 1970-01-01)
13) localtime It is used to adjust date to localtime, assuming the timestring was expressed in UTC
14) utc It is used to adjust date to utc, assuming the timestring was expressed in localtime

Example1: Retrieve Current Date:

SELECT strftime('%Y %m %d', 'now');
SELECT strftime('%Y-%m-%d %H:%M', 'now');

Output:

SQLite Strftime function 1 SQLite Strftime function 2

Example2: Retrieve First day of the month:

SELECT strftime('%Y-%m-%d', '2017-04-14', 'start of month');
SELECT strftime('%Y-%m-%d', 'now', 'start of month');
SELECT strftime('%Y-%m-%d', '2017-03-07', '-6 days');
SELECT strftime('%Y-%m-%d', 'now', '-13 days');

Output:

SQLite Strftime function 3 SQLite Strftime function 4 SQLite Strftime function 5 SQLite Strftime function 6

Example2: Retrieve Last day of the month:

SELECT strftime('%Y-%m-%d', '2017-03-07', 'start of month', '+1 month', '-1 day'); 
SELECT strftime('%Y-%m-%d', 'now', 'start of month', '+1 month', '-1 day');
SELECT strftime('%Y-%m-%d', '2017-03-07', '+24 days');
SELECT strftime('%Y-%m-%d', 'now', '+24 days');

Output:

SQLite Strftime function 7 SQLite Strftime function 8 SQLite Strftime function 9 SQLite Strftime function 10

Example3: Add/ Subtract years and days:

SELECT strftime('%Y-%m-%d', '2017-04-14', '+2 years');
SELECT strftime('%Y-%m-%d', 'now', '-2 years');
SELECT strftime('%Y-%m-%d', '2017-04-14', '+7 days');
SELECT strftime('%Y-%m-%d', 'now', '-10 days'); 

Output:

SQLite Strftime function 11 SQLite Strftime function 12 SQLite Strftime function 13 SQLite Strftime function 14
Next TopicSQLite Time




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf