Postgresql 专题
专题目录
您的位置:database > Postgresql专题 > PostgreSQL日期和时间函数
PostgreSQL日期和时间函数
作者:--    发布时间:2019-11-20

所有重要的日期和时间相关函数如下列表所示:

函数 描述
age() 减去参数
current date/time() 它指定当前日期和时间。
date_part() 获取子字段(相当于提取)
extract() 获得子字段
isfinite() 测试有限的日期,时间和间隔(非+/-无穷大)
justify 调整间隔

age(timestamp,timestamp)&age(timestamp):

函数 描述
age(timestamp, timestamp) 当使用第二个参数的时间戳形式调用时,age()减去参数,产生使用年数和月份的类型为“interval”的“符号”结果。
age(timestamp) 当仅使用时间戳作为参数调用时,age()current_date(午夜)减去。

age()函数例子

按ctrl + e打开查询编辑器,执行此查询示例:

select age(timestamp '2017-01-26', timestamp '1951-08-15');

执行上面语句得到以下结果 -

函数age(timestamp)的示例

它用于生产当前年龄。

执行上面语句得到以下结果 -

select age(timestamp '1990-08-15');

执行上面语句得到以下结果 -

当前date/time()

以下是返回与当前日期和时间相关的值的函数的列表。

函数 描述
current_date 提供当前日期
current_time 提供带时区的值
current_timestamp 提供带时区的值
current_time(precision) 可以选择使用precision参数,这将使结果在四分之一秒的范围内四舍五入到数位数。
current_timestamp(precision) 可以选择使用精度参数,这将使结果在四分之一秒的范围内四舍五入到数位数。
localtime 提供没有时区的值。
localtimestamp 提供没有时区的值。
localtime(precision) 可以选择使用精度参数,这将使结果在四分之一秒的范围内四舍五入到数位数。
localtimestamp(precision) 可以选择使用精度参数,这将使结果在四分之一秒的范围内四舍五入到数位数。

现在,您可以来看看以下命令:

获取当前时间:

select current_time;

获取当前日期:

select current_date;

获取当前时间戳(两者的日期和时间)

select current_timestamp;

当前时间戳更精确:

select current_timestamp(2);

本地时间戳:

select localtimestamp;


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册