This SQLite post explains how to use the SQLite now function (ie: timestring parameter) with syntax and examples.
In SQLite, there isn't a function called the now function, but rather "now" is a timestring parameter that is used in various SQLite functions to retrieve the current date and time. This may seem a bit confusing at first, since other SQL databases have a built-in function whose purpose is to return the current date and time. SQLite does this differently.
We will cover 3 syntaxes that show how to use the "now" timestring in SQLite.
The syntax for the now function (ie: timestring parameter) in SQLite is:
date('now')OR
time('now')OR
strftime(format, 'now')
Examples:
strftime('%Y-%m-%d','now')
strftime('%Y-%m-%d %H-%M','now')
strftime('%Y-%m-%d %H-%M-%S','now')It is used when expressing the current date/time using the strftime function. It can be one or more of the following:
| format | Explanation |
|---|---|
| %Y | Year as 4 digits (0000 to 9999) |
| %W | Week of year (00 to 53) |
| %w | Day of week (0 to 6, where 0 is Sunday) |
| %m | Month of year (01 to 12) |
| %d | Day of month (00 to 31) |
| %H | Hour (00 to 24) |
| %M | Minute (00 to 25) |
| %S | Seconds (00 to 59) |
| %s | Seconds since 1970-01-01 |
| %f | Fractional seconds (SS.SSS) |
| %j | Day of year (001 to 366) |
| %J | Julian day as a numeric value |
The now function can be used in the following versions of SQLite:
Let's look at some SQLite now function examples and explore how to use the now function in SQLite. You can use the built-in functions to retrieve the current date, current time, or current date/time values.
You could retrieve the current date in SQLite using the "now" timestring with either the date function or strftime function as follows:
sqlite> SELECT date('now');
Output: '2014-10-18' (formatted as YYYY-MM-DD)
sqlite> SELECT strftime('%Y-%m-%d','now');
Output: '2014-10-18' (formatted as YYYY-MM-DD)You could retrieve the current time in SQLite using the "now" timestring with either the time function or strftime function as follows:
sqlite> SELECT time('now');
Output: '11-13-52' (formatted as HH-MM-SS)
sqlite> SELECT strftime('%H-%M-%S','now');
Output: '11-13-52' (formatted as HH-MM-SS)
sqlite> SELECT strftime('%H-%M-%f','now');
Output: '11-13-52.990' (formatted as HH-MM-SS.SSS)
sqlite> SELECT strftime('%H-%M','now');
Output: '11-13' (formatted as HH-MM)You could retrieve the current date and time in SQLite using the "now" timestring with the strftime function as follows:
sqlite> SELECT strftime('%Y-%m-%d %H-%M','now');
Output: '2014-10-18 11-13' (formatted as YYYY-MM-DD HH-MM)
sqlite> SELECT strftime('%Y-%m-%d %H-%M-%S','now');
Output: '2014-10-18 11-13-52' (formatted as YYYY-MM-DD HH-MM-SS)
sqlite> SELECT strftime('%Y-%m-%d %H-%M-%f','now');
Output: '2014-10-18 11-13-52.990' (formatted as YYYY-MM-DD HH-MM-SS.SSS)