Unix timestamp in sqlite?

yayitswei picture yayitswei · Sep 12, 2010 · Viewed 47.7k times · Source

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

Answer

Daniel Vassallo picture Daniel Vassallo · Sep 12, 2010

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.