Date/Time Functions

The date/time functions provide a powerful set of tools for manipulating various date/time types.

Table 5-6. Date/Time Functions

FunctionReturnsDescriptionExample
abstime(timestamp)abstimeconvert to abstimeabstime(timestamp 'now')
age(timestamp)intervalpreserve months and yearsage(timestamp '1957-06-13')
age(timestamp,timestamp)intervalpreserve months and yearsage('now', timestamp '1957-06-13')
date_part(text,timestamp)float8portion of datedate_part('dow',timestamp 'now')
date_part(text,interval)float8portion of timedate_part('hour',interval '4 hrs 3 mins')
date_trunc(text,timestamp)timestamptruncate datedate_trunc('month',abstime 'now')
interval(reltime)intervalconvert to intervalinterval(reltime '4 hours')
isfinite(timestamp)boola finite time?isfinite(timestamp 'now')
isfinite(interval)boola finite time?isfinite(interval '4 hrs')
reltime(interval)reltimeconvert to reltimereltime(interval '4 hrs')
timestamp(date)timestampconvert to timestamptimestamp(date 'today')
timestamp(date,time)timestampconvert to timestamptimestamp(timestamp '1998-02-24',time '23:07');
to_char(timestamp,text)textconvert to stringto_char(timestamp '1998-02-24','DD');

For the date_part and date_trunc functions, arguments can be `year', `month', `day', `hour', `minute', and `second', as well as the more specialized quantities `decade', `century', `millennium', `millisecond', and `microsecond'. date_part allows `dow' to return day of week, 'week' to return the ISO-defined week of year, and `epoch' to return seconds since 1970 (for timestamp) or 'epoch' to return total elapsed seconds (for interval).