J. Doe
J. Doe

Reputation: 91

SQLite3 - convert human date to epoch

I have dates in US format, and my database supports epoch (unixtime). Is it possible to convert using the SQLite3 query?

INSERT INTO candles_USD_BCH (id, timestamp) values (null, 2018-03-31 01:02:03);

Upvotes: 1

Views: 5766

Answers (1)

INSERT INTO candles_USD_BCH (id, timestamp) 
  values (null, CAST(strftime('%s', '2018-03-31 01:02:03') as integer));

The CAST() is probably not essential in SQLite, but I think it's a good habit. The function strftime() returns a string.

SQLite Date and Time functions, and pay attention to the section "Caveats and Bugs".

Upvotes: 3

Related Questions