Unix timestamp in sqlite?

Viewed 59366

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
4 Answers
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

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

For some reason (that's undoubtedly my fault) Daniel's julianday() component doesn't quite do it for me, but resorting to the following jankery appears to do the trick when selecting from a timestamp field called tmstmp:

select
 strftime('%s', a.tmstmp) + strftime('%f', a.tmstmp)
   as unix_time
from any_table a
Related