Skip to content

DAYS_BETWEEN

Return the number of days between two dates. The result may be positive or negative.

DAYS_BETWEEN ( date1, date2 )

The inputs may have DATE, TIMESTAMP, or TIMESTAMPTZ data types. If one of the inputs is null, the result is null.

If the first date is later than the second date, the result is a positive number; otherwise, the result is negative. For example, depending on the order of date expressions in this example, the function returns a positive number or a negative number:

premdb=# select matchday, days_between(current_timestamp, matchday) from match where htid=2 and atid=60;
     matchday       | days_between 
---------------------+--------------
 2010-03-06 00:00:00 |         3055
(1 row)

premdb=# select matchday, days_between(matchday, current_timestamp) from match where htid=2 and atid=60;
     matchday       | days_between 
---------------------+--------------
 2010-03-06 00:00:00 |        -3055
(1 row)

Fractional numbers of hours are ignored. This function always returns whole numbers. For example:

premdb=# select matchday, days_between(current_timestamp, matchday+interval '12 hours') from match where htid=3 and atid=53;
     matchday       | days_between 
---------------------+--------------
 1998-03-11 00:00:00 |         7433
(1 row)

Parent topic:Datetime Functions