51

How do you obtain the current timestamp in Sqlite? current_time, current_date, current_timestamp both return formatted dates, instead of a long.

sqlite> insert into events (timestamp) values (current_timestamp);
sqlite> insert into events (timestamp) values (current_date);
sqlite> insert into events (timestamp) values (current_time);
sqlite> select * from events;
1|2010-09-11 23:18:38
2|2010-09-11
3|23:18:51

What I want:

4|23234232
yayitswei
  • 4,587
  • 5
  • 27
  • 34

3 Answers3

129

The docs mention this method:

SELECT strftime('%s', 'now');
1284248196

And this one which includes the fractional part:

SELECT (julianday('now') - 2440587.5) * 86400.0;
1284248196.65098

Both represent Unix Time, the number of seconds passed since January 1, 1970.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 7
    Still handy info after 10 years! – axello Feb 23 '20 at 09:26
  • 1
    The first one may need casting to a number before numeric comparisons! For example, a quick and dirty `1*strftime('%s', 'now')` might be enough, or you can use the proper `CAST( strftime('%s', 'now') AS INT )` syntax. – biziclop Jul 07 '22 at 14:27
2

SQLite now has unixepoch function that returns a unix timestamp (https://www.sqlite.org/lang_datefunc.html)

However right now I don't recommend it because some things like Sqlitebrowser and Entity Framework (6.0.8) don't seem to support it yet. Right now it's still better to use old strftime from accepted answer

0
select strftime('%W'),date('now'),date(),datetime(),strftime('%s', 'now');

result in

strftime('%W')  -   date('now')  -  date()   -  datetime()   -     strftime('%s', 'now')
    08       -      2021-02-23   -  2021-02-23 - 2021-02-23  15:35:12-  1614094512
Hakan
  • 240
  • 3
  • 4