sqlite 时间戳格式化

发布于 2024-09-13 07:44:30 字数 380 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2024-09-20 07:44:30

您需要首先将时间戳转换为日期时间:

SELECT strftime('%d - %m  - %Y ', datetime(1281353727, 'unixepoch')) FROM Visits;

You need to convert the timestamp to datetime first:

SELECT strftime('%d - %m  - %Y ', datetime(1281353727, 'unixepoch')) FROM Visits;
药祭#氼 2024-09-20 07:44:30

这对我有用:

datetime(visit_date/1000000,'unixepoch')

This worked for me:

datetime(visit_date/1000000,'unixepoch')
嗫嚅 2024-09-20 07:44:30

将时间戳转换为日期时间,然后对其进行格式化:

SELECT strftime('%d - %m  - %Y ', datetime(1281353727, 'unixepoch', 'localtime')) FROM Visits;

catch 1

确保添加 localtime 参数。否则,您将得到居住在“格林威治”的人们所认为的日期(因为时间戳从 1/1/1970 00:00 UTC (=GMT) 开始计数)。

请参阅下文,在具有 EEST 时区的工作站上,日期输出如何根据我们是否使用“本地时间”而变化。

select strftime('%d - %m  - %Y ', datetime(1623707777, 'unixepoch', 'localtime'))

输出:2021 年 15 月 6 日

select strftime('%d - %m  - %Y ', datetime(1623707777, 'unixepoch'))

输出:2021 年 14 月 6 日

这是因为在 EEST 00:01,GMT 仍为昨天。

catch 2

许多程序(例如 ViberPC)也会在时间戳中添加毫秒(甚至微秒),在这种情况下,您需要首先除以 1000(或分别除以 1000000):

select strftime('%d - %m  - %Y ', datetime(1623707777421/1000, 'unixepoch', 'localtime'))

Convert the timestamp to datetime and then format it:

SELECT strftime('%d - %m  - %Y ', datetime(1281353727, 'unixepoch', 'localtime')) FROM Visits;

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.

select strftime('%d - %m  - %Y ', datetime(1623707777, 'unixepoch', 'localtime'))

output: 15 - 06 - 2021

select strftime('%d - %m  - %Y ', datetime(1623707777, 'unixepoch'))

output: 14 - 06 - 2021

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):

select strftime('%d - %m  - %Y ', datetime(1623707777421/1000, 'unixepoch', 'localtime'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文