postgresql 如何在 Postgres 中将日期时间转换为 unix 纪元值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27740363/
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
How to convert date time into unix epoch value in Postgres?
提问by Ashish Kumar Saxena
How do I convert the following format to UNIX timestamps?
如何将以下格式转换为 UNIX 时间戳?
A value like: 01-02-2015 10:20 PM
should be converted to: 1418273999000
像:这样的值01-02-2015 10:20 PM
应该转换为:1418273999000
I did try to_timestamp
function but its not working for me.
我确实尝试过to_timestamp
函数,但它对我不起作用。
回答by Joe Love
If your data is stored in a column called ts, in a table called data, do this:
如果您的数据存储在名为 ts 的列中,在名为 data 的表中,请执行以下操作:
select extract(epoch from ts) from data
回答by haoming
To add Joe's answer, you can use date_part
, i think it's syntax is clearer than 'extract'.
要添加 Joe 的答案,您可以使用date_part
,我认为它的语法比“提取”更清晰。
select date_part('epoch', ts) from data;
select date_part('epoch', ts) from data;