sqlite 时间戳格式化
我正在尝试使用 sqlite 数据库中的日期。我将日期存储为时间戳,但是当我使用 strftime() 将它们格式化为人类可读的日期时,我得到了意想不到的结果。
考虑以下情况,我选择当前时间戳:
SELECT strftime("%s","now");
1281353727
然后我尝试使用我知道代表的时间戳来格式化日期,现在期望返回今天日期的人类可读格式:
SELECT strftime('%d - %m - %Y ', 1281353727);
01 - 04 - 3503
相反,我得到了上述结果。这是正确的行为吗?我做错了什么吗?
预先感谢,
凯文
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要首先将时间戳转换为日期时间:
You need to convert the timestamp to datetime first:
这对我有用:
This worked for me:
将时间戳转换为日期时间,然后对其进行格式化:
catch 1
确保添加
localtime
参数。否则,您将得到居住在“格林威治”的人们所认为的日期(因为时间戳从 1/1/1970 00:00 UTC (=GMT) 开始计数)。请参阅下文,在具有 EEST 时区的工作站上,日期输出如何根据我们是否使用“本地时间”而变化。
这是因为在 EEST 00:01,GMT 仍为昨天。
catch 2
许多程序(例如 ViberPC)也会在时间戳中添加毫秒(甚至微秒),在这种情况下,您需要首先除以 1000(或分别除以 1000000):
Convert the timestamp to datetime and then format it:
catch 1
Make sure you add the
localtime
argument. Otherwise you will get a date as perceived by the people living in "Greenwich" (since timestamp started counting at 1/1/1970 00:00 UTC (=GMT) ).See below how the date output changes depending on whether we use 'localtime', on a workstation with EEST timezone.
This is Because at 00:01 EEST, the GMT is still at yesterday.
catch 2
Many programs (e.g. ViberPC) add milliseconds (or even microseconds) also to timestamp, in which case you need to first divide by 1000 (or by 1000000 respectively):