SQL sqlite时间戳格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3439624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlite timestamp formatting
提问by Kevin Bradshaw
I am trying to work with dates in an sqlite database. I am storing my dates as timestamps, but when I use strftime() to format them to human readable dates I am getting back unxpected results.
我正在尝试使用 sqlite 数据库中的日期。我将日期存储为时间戳,但是当我使用 strftime() 将它们格式化为人类可读的日期时,我得到了意想不到的结果。
Consider the following, I select the current timestamp:
考虑以下,我选择当前时间戳:
SELECT strftime("%s","now");
1281353727
Then I try to format a date using the timestamp that I know to represent now expecting to get back a human readable format of todays date:
然后我尝试使用我知道的时间戳来格式化日期,现在希望得到今天日期的人类可读格式:
SELECT strftime('%d - %m - %Y ', 1281353727);
01 - 04 - 3503
Instead I get the above result. Is this correct behaviour? am I doing something wrong?
相反,我得到了上述结果。这是正确的行为吗?难道我做错了什么?
Thanks in advance,
提前致谢,
Kevin
凯文
回答by reko_t
You need to convert the timestamp to datetime first:
您需要先将时间戳转换为日期时间:
SELECT strftime('%d - %m - %Y ', datetime(1281353727, 'unixepoch')) FROM Visits;
回答by Sergio Abreu
This worked for me:
这对我有用:
datetime(visit_date/1000000,'unixepoch')