PostgreSQL常用时间函数与时间计算提取示例说明

文章目录

常用函数与常量

函数

说明

current_time

现在的时间

current_date

今天的日期

current_timestamp

当前日期和时间

now()

当前的日期和时间和current_timestamp一样

localtime

time,当前时间

localtimestamp

当前日期和时间

timeofday()

字符串,当前日期和时间

age(timestamp1, timestamp2)

计算年纪,timestamp1-timestamp2,结果类型是interval,类似于:29 years 9 mons 27 days

select current_date,current_time,current_timestamp,now(),localtime,localtimestamp,timeofday();

PostgreSQL常用时间变量

select age('2030-04-10', '2000-06-13');

第1个参数减第2个参数,age返回值是interval,很方便算年龄

PostgreSQL age函数

-- 时间戳转字符串再截取
SELECT now()::timestamp,substring(''||now()::timestamp from 1 for 19);

PostgreSQL时间截取

to_timestamp(字符串转时间戳、数字转时间戳)

函数

说明

to_timestamp(text, text)