如何在Postgres中将日期时间转换为unix epoch值?

Ash*_*ena 27 postgresql datetime unix-timestamp

如何将以下格式转换为UNIX时间戳?

像:的值01-02-2015 10:20 PM应该转换为:1418273999000

我确实试过to_timestamp功能,但它不适合我.

Joe*_*ove 50

如果您的数据存储在名为ts的列中,则在名为data的表中,执行以下操作:

select extract(epoch from ts) from data
Run Code Online (Sandbox Code Playgroud)

  • 这一切都在手册中:http://www.postgresql.org/docs/current/interactive/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT (2认同)

小智 8

要添加 Joe 的答案,您可以使用date_part,我认为它的语法比“提取”更清晰。

select date_part('epoch', ts) from data;