PostgreSQL:如何从Unix纪元转换为日期? [英] PostgreSQL: how to convert from Unix epoch to date?
本文介绍了PostgreSQL:如何从Unix纪元转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这句话给了我日期和时间。
The statement gives me the date and time.
如何修改语句,使其仅返回日期(而不是时间)?
How could I modify the statement so that it returns only the date (and not the time)?
SELECT to_timestamp( TRUNC( CAST( epoch_ms AS bigint ) / 1000 ) );
推荐答案
/* Current time */
select now();
/* Epoch from current time;
Epoch is number of seconds since 1970-01-01 00:00:00+00 */
select extract(epoch from now());
/* Get back time from epoch */
-- Option 1 - use to_timestamp function
select to_timestamp( extract(epoch from now()));
-- Option 2 - add seconds to 'epoch'
select timestamp with time zone 'epoch'
+ extract(epoch from now()) * interval '1 second';
/* Cast timestamp to date */
-- Based on Option 1
select to_timestamp(extract(epoch from now()))::date;
-- Based on Option 2
select (timestamp with time zone 'epoch'
+ extract(epoch from now()) * interval '1 second')::date;
/* For column epoch_ms */
select to_timestamp(extract(epoch epoch_ms))::date;
这篇关于PostgreSQL:如何从Unix纪元转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文