How to convert 2001 based timestamp to datetime in SQLite?

旧巷老猫 提交于 2019-12-23 05:38:09

问题


ZPUBLICATIONDATETIME is of type TIMESTAMP.

So when I do this:

SELECT strftime('%d - %m  - %Y ', datetime(ZPUBLICATIONDATETIME, 'unixepoch')) FROM ZTNNEWS;

I get 26 - 05 - 1984 instead of 2015. iOS (Core Data) writes datetime on 1 Jan 2001 based. What is the best approach to get the right date conversion?

Shall I just add 31 years to it or is there an alternative to unixepoch to put in there?

Essentially what I am trying to do is to get the records from past two days:

select * 
from ZTNNEWS
where DATETIME(ZPUBLICATIONDATETIME) >  DATETIME('now', '-2 day')

But because ZPUBLICATIONDATETIME is of type TIMESTAMP rather than Datetime, it doesn't output anything.

Any advice please?


回答1:


Just had the same problem. Adding the date value to the seconds of 1 Jan 2001 seems to do the job:

SELECT * FROM ztnnews WHERE DATETIME(zpublicationtime + 978307200) > DATETIME('now', '-2 day');

I used ruby to get the seconds of 1 Jan 2001:

$ irb
2.2.3 :001 > require "time"
 => true 
2.2.3 :002 > Time.parse( "2001-01-01T00:00:00Z").to_i
 => 978307200 


来源:https://stackoverflow.com/questions/30536183/how-to-convert-2001-based-timestamp-to-datetime-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!