Postgres

在Postgres中timestamp和时间函数配合

小樊
99
2024-07-19 16:51:40
栏目: 编程语言

在Postgres中,可以使用时间函数来操作timestamp类型的数据。以下是一些常用的时间函数:

  1. now(): 返回当前日期和时间
SELECT now();
  1. date_trunc(): 将时间戳戳截断到指定的时间单位
SELECT date_trunc('hour', now());
  1. extract(): 提取时间戳中的特定部分,比如年、月、日、时、分、秒等
SELECT extract(year from now());
  1. age(): 计算两个时间戳之间的间隔
SELECT age('2022-01-01'::timestamp, now());
  1. timestampadd(): 在时间戳上添加一个时间间隔
SELECT now() + interval '1 day';
  1. timestampdiff(): 计算两个时间戳之间的时间间隔
SELECT timestamp '2022-01-01' - now();

这些时间函数可以与timestamp类型的数据一起使用,用于对时间戳进行各种操作和计算。

0
看了该问题的人还看了