PostgreSQL 時間/日期函數和操作符

日期/時間操做符

下表演示了基本算術操作符的行為(+,*, 等):

操作符例子結果
+ 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)intervalcurrent_date減去參數后的結果(在午夜)age(timestamp '1957-06-13')43 years 8 mons 3 days
clock_timestamp() timestamp with time zone實時時鐘的當前時間戳(在語句執行時變化)   
current_date date當前的日期;   
current_time time with time zone當日時間;   
current_timestamp timestamp with time zone當前事務開始時的時間戳;   
date_part(text, timestamp) double precision獲取子域(等效于extract); date_part('hour', timestamp '2001-02-16 20:38:40')20
date_part(text, interval)double precision獲取子域(等效于extract); date_part('month', interval '2 years 3 months')3
date_trunc(text, timestamp) timestamp截斷成指定的精度; date_trunc('hour', timestamp '2001-02-16 20:38:40')2001-02-16 20:00:00
date_trunc(text, interval)interval截取指定的精度, date_trunc('hour', interval '2 days 3 hours 40 minutes')2 days 03:00:00
extract(field from timestamp) double precision獲取子域; extract(hour from timestamp '2001-02-16 20:38:40')20
extract(field from interval)double precision獲取子域; extract(month from interval '2 years 3 months')3
isfinite(date) boolean測試是否為有窮日期(不是 +/-無窮)isfinite(date '2001-02-16')true
isfinite(timestamp)boolean測試是否為有窮時間戳(不是 +/-無窮)isfinite(timestamp '2001-02-16 21:28:30')true
isfinite(interval)boolean測試是否為有窮時間間隔isfinite(interval '4 hours')true
justify_days(interval) interval按照每月 30 天調整時間間隔justify_days(interval '35 days')1 mon 5 days
justify_hours(interval) interval按照每天 24 小時調整時間間隔justify_hours(interval '27 hours')1 day 03:00:00
justify_interval(interval) interval使用justify_daysjustify_hours調整時間間隔的同時進行正負號調整justify_interval(interval '1 mon -1 hour')29 days 23:00:00
localtime time當日時間;   
localtimestamp timestamp當前事務開始時的時間戳;   
make_date(year int, month int, day int) date 為年、月和日字段創建日期 make_date(2013, 7, 15)2013-07-15
make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0) interval 從年、月、周、天、小時、分鐘和秒字段中創建間隔 make_interval(days := 10)10 days
make_time(hour int, min int, sec double precision) time 從小時、分鐘和秒字段中創建時間 make_time(8, 15, 23.5)08:15:23.5
make_timestamp(year int, month int, day int, hour int, min int, sec double precision) timestamp 從年、月、日、小時、分鐘和秒字段中創建時間戳 make_timestamp(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5
make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ]) timestamp with time zone 從年、月、日、小時、分鐘和秒字段中創建帶有時區的時間戳。 沒有指定timezone時,使用當前的時區。 make_timestamptz(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5+01
now() timestamp with time zone當前事務開始時的時間戳;   
statement_timestamp() timestamp with time zone實時時鐘的當前時間戳;   
timeofday() textclock_timestamp相同,但結果是一個text 字符串;   
transaction_timestamp() timestamp with time zone當前事務開始時的時間戳;