92

声明给了我日期和时间。

我如何修改语句以使其仅返回日期(而不是时间)?

SELECT to_timestamp( TRUNC( CAST( epoch_ms AS bigint ) / 1000 ) );
4

6 回答 6

152

您使用to_timestamp函数,然后将时间戳转换为date

 select to_timestamp(epoch_column)::date;

更多细节:

/* Current time */
 select now();  -- returns timestamp

/* 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; 

在你的情况下:

 select to_timestamp(epoch_ms / 1000)::date;

PostgreSQL 文档

于 2013-05-17T13:35:54.473 回答
26
select to_timestamp(cast(epoch_ms/1000 as bigint))::date

为我工作

于 2020-01-27T14:49:10.260 回答
4

在 Postgres 10 上:

SELECT to_timestamp(CAST(epoch_ms as bigint)/1000)

于 2020-06-25T21:56:46.877 回答
2

上面的解决方案不适用于 PostgreSQL 上的最新版本。我发现这种方法可以转换存储在数字中的纪元时间,而 int 列类型在 PostgreSQL 13 上:

SELECT TIMESTAMP 'epoch' + (<table>.field::int) * INTERVAL '1 second' as started_on from <table>;

有关更详细的说明,您可以在此处查看https://www.yodiw.com/convert-epoch-time-to-timestamp-in-postgresql/#more-214

于 2020-05-27T17:25:57.830 回答
1

这对我来说很好:

SELECT t.*,
   to_timestamp(cast(t.prev_fire_time/1000 as bigint)) as prev_fire_time,
   to_timestamp(cast(t.next_fire_time/1000 as bigint)) as next_fire_time,
   to_timestamp(cast(t.start_time/1000 as bigint)) as start_time
FROM public.qrtz_triggers t;
于 2021-03-10T15:33:34.747 回答
0

自 GNU 纪元以来的秒数date

$ date +%s.%N
1627059870.945134901

这适用于 PostgreSQL 11:

# select to_timestamp (1627059870.945134901);
         to_timestamp          
-------------------------------
 2021-07-23 19:04:30.945135+02
(1 row)

# select to_timestamp (1627059870.945134901)::date;
 to_timestamp 
--------------
 2021-07-23
(1 row)
于 2021-07-23T17:11:29.713 回答