我有一个以毫秒为单位的日期。获得类似:
SELECT EXTRACT(MILLISECONDS FROM NOW())
如何再次恢复约会?
我尝试过:
SELECT TO_TIMESTAMP(EXTRACT(MILLISECONDS FROM NOW()))
上面返回 1970 年的日期...
EXTRACT(MILLISECONDS ...)
帮助您(根据文档)...
秒字段,包括小数部分,乘以 1000。 请注意,这包括整秒。
只需测试:
SELECT now(), EXTRACT(MILLISECONDS FROM NOW()) AS millisec;
其余部分也在手册中:
纪元
对于
值,自 1970-01-01 00:00:00 UTC 以来的秒数(可以为负数);对于timestamp with time zone
和date
值,自 1970-01-01 00:00:00 当地时间以来的秒数; 对于timestamp
值,间隔中的总秒数interval
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08'); *Result: 982384720.12* SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours'); *Result: 442800*
以下是将纪元值转换回时间戳的方法:
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720.12 * INTERVAL '1 second';
(
函数封装了上面的转换。)to_timestamp
EXTRACT 无法以毫秒为单位获取日期。 我认为这应该有效:
SELECT EXTRACT(EPOCH FROM NOW()) * 1000;
SELECT TO_TIMESTAMP(EXTRACT(EPOCH FROM NOW()))
其他答案略有改进:
SELECT NOW() AS NOW_TIMESTAMP,
EXTRACT(EPOCH FROM NOW()) AS NOW_EPOCH_SECONDS_DOUBLE,
EXTRACT(EPOCH FROM NOW())::double precision*1000 AS NOW_EPOCH_MILLI_DOUBLE,
(EXTRACT(EPOCH FROM NOW())::double precision*1000)::bigint as NOW_EPOCH_MILLI_LONG,
TO_TIMESTAMP(1719501208167::double precision / 1000) as TO_DATETIME
结果:
now_时间戳 | now_epoch_seconds_double | now_epoch_milli_double | now_epoch_milli_long | 到_日期时间 |
---|---|---|---|---|
2024-06-28 14:51:26.566297+00 | 1719586286.566297 | 1719586286566.297 | 1719586286566 | 2024-06-27 15:13:28.167+00 |