【PostgreSQL】时间戳处理相关函数和操作

Operator Example Result
+ date '2001-09-28' + integer '7' date '2001-10-05'
+ date '2001-09-28' + interval '1 hour' timestamp '2001-09-28 01:00:00'
+ date '2001-09-28' + time '03:00' timestamp '2001-09-28 03:00:00'
+ interval '1 day' + interval '1 hour' interval '1 day 01:00:00'
+ timestamp '2001-09-28 01:00' + interval '23 hours' timestamp '2001-09-29 00:00:00'
+ time '01:00' + interval '3 hours' time '04:00:00'
- - interval '23 hours' interval '-23:00:00'
- date '2001-10-01' - date '2001-09-28' integer '3' (days)
- date '2001-10-01' - integer '7' date '2001-09-24'
- date '2001-09-28' - interval '1 hour' timestamp '2001-09-27 23:00:00'
- time '05:00' - time '03:00' interval '02:00:00'
- time '05:00' - interval '2 hours' time '03:00:00'
- timestamp '2001-09-28 23:00' - interval '23 hours' timestamp '2001-09-28 00:00:00'
- interval '1 day' - interval '1 hour' interval '1 day -01:00:00'
- timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' interval '1 day 15:00:00'
* 900 * interval '1 second' interval '00:15:00'
* 21 * interval '1 day' interval '21 days'
* double precision '3.5' * interval '1 hour' interval '03:30:00'
/ interval '1 hour' / double precision '1.5' interval '00:40:00'

常用函数

函数 返回类型 描述 样例 结果
age(timestamp, timestamp) interval 返回两个时间戳之间相差的年月天数 age(timestamp '2001-04-10', timestamp '1957-06-13') 43 years 9 mons 27 days
age(timestamp) interval 返回现在的时间与参数的中时间戳的差值 age(timestamp '1957-06-13') 43 years 8 mons 3 days
extract(field from timestamp) double precision 得到时间戳里的任意一个值,比如年,月或日 extract(hour from timestamp '2001-02-16 20:38:40') 20
make_date(year int, month int, day int) date 自己创建一个日期包含 year, month and day fields make_date(2013, 7, 15) 2013-07-15
make_timestamp(year int, month int, day int, hour int, min int, sec double precision) timestamp 自己创建一个时间戳包含year, month, day, hour, minute and seconds fields make_timestamp(2013, 7, 15, 8, 15, 23.5) 2013-07-15 08:15:23.5
now() timestamp with time zone 此时此刻的时间戳    
to_timestamp(double precision) timestamp with time zone 将格林尼治时间秒转化为时间戳 ( 秒是从 1970-01-01 00:00:00+00开始计的) to_timestamp(1284352323) 2010-09-13 04:32:03+00

你可能感兴趣的:(数据库)