English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PostgreSQL Time/Date functions and operators

Date/Time operators

The following demonstrates the behavior of basic arithmetic operators (+,*, etc.):

operatorExampleResult
+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'

Date/Time function

FunctionReturn typeDescriptionExampleResult
age(timestamp, timestamp)intervalThe 'symbolized' result after subtracting the parameter, using years and months, not just daysage(timestamp '2001-04-10', timestamp '1957-06-13')43 years 9 mons 27 days
age(timestamp)intervalThe result after subtracting the parameter from current_date (at midnight)age(timestamp '1957-06-13')43 years 8 mons 3 days
clock_timestamptimestamp with time zoneThe current timestamp of the real-time clock (changes at the time of statement execution)}  
current_datedateThe current date;  
current_timetime with time zoneCurrent day time;  
current_timestamptimestamp with time zoneTimestamp when the current transaction begins;  
date_part(text, timestamp)double precisionGet subdomain (equivalent to extract);date_part('hour', timestamp '2001-02-16 20:38:40')20
date_part(text, interval)double precisionGet subdomain (equivalent to extract);date_part('month', interval '2 years 3 months')3
date_trunc(text, timestamp)timestampTruncate to the specified precision;date_trunc('hour', timestamp '2001-02-16 20:38:40')2001-02-16 20:00:00
date_trunc(text, interval)intervalTruncate to the specified precision,date_trunc('hour', interval '2 days 3 hours 40 minutes')2 days 03:00:00
extract(field from         timestamp)double precisionGet subdomain;extract(hour from timestamp '2001-02-16 20:38:40')20
extract(field from         interval)double precisionGet subdomain;extract(month from interval '2 years 3 months')3
isfinite(date)booleanTest whether the date is finite (not +/-Infinite)isfinite(date '2001-02-16')true
isfinite(timestamp)booleanTest whether the timestamp is finite (not +/-Infinite)isfinite(timestamp '2001-02-16 21:28:30')true
isfinite(interval)booleanTest whether the time interval is finiteisfinite(interval '4 hours')true
justify_days(interval)intervalAccording to each month 30 days adjust the time intervaljustify_days(interval '35 days')1 mon 5 days
justify_hours(interval)intervalAccording to each day 24 Adjust the time interval by hoursjustify_hours(interval '27 hours')1 day 03:00:00
justify_interval(interval)intervalAdjust the time interval using justify_days and justify_hours while adjusting the signjustify_interval(interval '1 mon -1 hour')29 days 23:00:00
localtimetimeCurrent day time;  
localtimestamptimestampTimestamp when the current transaction begins;  
            make_date(year int,             month int,             day int) dateCreate a date for the year, month, and day fieldsmake_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)           intervalCreate an interval from the year, month, week, day, hour, minute, and second fieldsmake_interval(days := 10)10 days
          make_time(hour int,           min int,           sec double precision)           timeCreate a time from the hour, minute, and second fieldsmake_time(8, 15, 23.5)08:15:23.5
          make_timestamp(year int,           month int,           day int,           hour int,           min int,           sec double precision)           timestampCreate a timestamp from the year, month, day, hour, minute, and second fieldsmake_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 zoneCreate a timestamp with timezone from the year, month, day, hour, minute, and second fields.         Use the current timezone if no timezone is specified.make_timestamptz(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5+01
now()timestamp with time zoneTimestamp when the current transaction begins;  
statement_timestamp()timestamp with time zoneCurrent timestamp of the real-time clock;  
timeofday()textSame as clock_timestamp, but the result is a text String;  
transaction_timestamp()timestamp with time zoneTimestamp when the current transaction begins;