SoFunction
Updated on 2025-04-08

Detailed explanation of the date-time difference DATEDIFF instance of PostgreSQL

PostgreSQL-DATEDIFF-Date time difference in seconds, days, months, weeks, etc.

You can calculate the difference between two date-time values ​​in seconds, minutes, hours, days, weeks, months, and years in PostgreSQL using various date-time expressions or user-defined DATEDIFF functions (UDFs).

Overview

PostgreSQL does not provide [2] DATEDIFF functions similar to SQL Server DATEDIFF, but you can use various expressions or UDFs to get the same results.

SQL Server
and Sybase
PostgreSQL
Years DATEDIFF(yy, start, end) DATE_PART('year', end) - DATE_PART('year', start)
Months DATEDIFF(mm, start, end) years_diff * 12 + (DATE_PART('month', end) - DATE_PART('month', start))
Days DATEDIFF(dd, start, end) DATE_PART('day', end - start)
Weeks DATEDIFF(wk, start, end) TRUNC(DATE_PART('day', end - start)/7)
Hours DATEDIFF(hh, start, end) days_diff * 24 + DATE_PART('hour', end - start )
Minutes DATEDIFF(mi, start, end) hours_diff * 60 + DATE_PART('minute', end - start )
Seconds DATEDIFF(ss, start, end) minutes_diff * 60 + DATE_PART('minute', end - start )

PostgreSQL - Date Differences in Year

Consider using the SQL Server function to calculate the difference between two dates in years:

SQL Server

  -- Difference between Oct 02, 2011 and Jan 01, 2012 in years
  SELECT DATEDIFF(year, '2011-10-02', '2012-01-01');
  -- Result: 1

Note that the SQL Server DATEDIFF function returns 1 year, although there are only 3 months between dates.

SQL Server does not count the entire year that passes between dates, it only calculates the differences between years.

In PostgreSQL, you can take the year part from the date and subtract it.

PostgreSQL

 -- Difference between Oct 02, 2011 and Jan 01, 2012 in years
  SELECT DATE_PART('year', '2012-01-01'::date) - DATE_PART('year', '2011-10-02'::date);
  -- Result: 1

PostgreSQL - Date Differences in Month

Consider using the SQL Server function to calculate the difference between two dates (in months):

SQL Server

  -- Difference between Oct 02, 2011 and Jan 01, 2012 in months
  SELECT DATEDIFF(month, '2011-10-02', '2012-01-01');
  -- Result: 3

In PostgreSQL, you can multiply the difference between years by 12 and then add the difference between month parts (can be negative).

PostgreSQL

-- Difference between Oct 02, 2011 and Jan 01, 2012 in months
  SELECT (DATE_PART('year', '2012-01-01'::date) - DATE_PART('year', '2011-10-02'::date)) * 12 +
              (DATE_PART('month', '2012-01-01'::date) - DATE_PART('month', '2011-10-02'::date));
  -- Result: 3

PostgreSQL - Date Day Difference

Consider using SQL Server functions to calculate the date difference between two days:

SQL Server

 -- Difference between Dec 29, 2011 23:00 and Dec 31, 2011 01:00 in days
  SELECT DATEDIFF(day, '2011-12-29 23:00:00', '2011-12-31 01:00:00');
  -- Result: 2

Note that DATEDIFF returns 2 days, although there is only 1 day and 2 hours between datetime values.

In PostgreSQL, if you subtract a date-time value (TIMESTAMP, DATE, or TIME data type) from another, you will get an INTERVAL value in the format " ddd days hh:mi:ss ".

   SELECT '2011-12-31 01:00:00'::timestamp - '2011-12-29 23:00:00'::timestamp;
    -- Result: "1 day 02:00:00"
 
    SELECT '2011-12-31 01:00:00'::timestamp - '2010-09-17 23:00:00'::timestamp;
    -- Result: "469 days 02:00:00"

So, you can use the date_part number function extract the number of days, but it returns the number of days between the full number of days.

PostgreSQL

 -- Difference between Dec 29, 2011 23:00 and Dec 31, 2011 01:00 in days
  SELECT DATE_PART('day', '2011-12-31 01:00:00'::timestamp - '2011-12-29 23:00:00'::timestamp);
  -- Result: 1

PostgreSQL - Date Differences in the Midweek

Consider using the SQL Server function to calculate the difference between two dates in a two-week period:

SQL Server

-- Difference between Dec 22, 2011 and Dec 31, 2011 in weeks
  SELECT DATEDIFF(week, '2011-12-22', '2011-12-31');
  -- Result: 1

DATEDIFF Returns the number of weeks between date and time values.

In PostgreSQL, you can use an expression to define the number of days (see above) and divide it by 7. TRUNC is required to delete the divided decimal part.

PostgreSQL

  -- Difference between Dec 22, 2011 and Dec 31, 2011 in weeks
  SELECT TRUNC(DATE_PART('day', '2011-12-31'::timestamp - '2011-12-22'::timestamp)/7);
  -- Result: 1

PostgreSQL - Hour Differences in Date Time

Consider using the SQL Server function to calculate the time difference between two datetime values, in hours:

SQL Server

  -- Difference between Dec 30, 2011 08:55 and Dec 30, 2011 9:05 in weeks
  SELECT DATEDIFF(hour, '2011-12-30 08:55', '2011-12-30 09:05');
  -- Result: 1

Note that although there is only a 10-minute difference between datetime values, DATEDIFF returns 1 hour.

In PostgreSQL, you can use expressions to define the number of days (see above), multiply by 24 and by hours.

PostgreSQL

 -- Difference between Dec 30, 2011 08:55 and Dec 30, 2011 9:05 in weeks
  SELECT DATE_PART('day', '2011-12-30 08:55'::timestamp - '2011-12-30 09:05'::timestamp) * 24 +
              DATE_PART('hour', '2011-12-30 08:55'::timestamp - '2011-12-30 09:05'::timestamp);
  -- Result: 0

Note that this PostreSQL expression returns the full number of hours passed between datetime values.

PostgreSQL - Date time difference in minutes

Consider using the SQL Server function to calculate the difference between two date-time values ​​in minutes:

SQL Server

  -- Difference between Dec 30, 2011 08:54:55 and  Dec 30, 2011 08:56:10 in minutes
  SELECT DATEDIFF(minute, '2011-12-30 08:54:55', '2011-12-30 08:56:10');
  -- Result: 2
 
  -- Time only
  SELECT DATEDIFF(minute, '08:54:55', '08:56:10');
  -- Result: 2

Note that although the datetime values ​​are only 1 minute and 15 seconds, DATEDIFF returns 2 minutes.

In PostgreSQL, you can use an expression to define the number of hours (see above), multiply by 60 and by minutes.

PostgreSQL

-- Difference between Dec 30, 2011 08:54:55 and  Dec 30, 2011 08:56:10 in minutes
  SELECT (DATE_PART('day', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp) * 24 +
               DATE_PART('hour', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp)) * 60 +
               DATE_PART('minute', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp);
  -- Result: 1
 
  -- Time only
  SELECT DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 +
              DATE_PART('minute', '08:56:10'::time - '08:54:55'::time);
  -- Result: 1

Note that these PostreSQL expressions return the full number of minutes passed between datetime values.

PostgreSQL - Date time difference (in seconds)

Consider using the SQL Server function to calculate the difference between two date-time values ​​in seconds:

SQL Server

  -- Difference between Dec 30, 2011 08:54:55 and  Dec 30, 2011 08:56:10 in seconds
  SELECT DATEDIFF(second, '2011-12-30 08:54:55', '2011-12-30 08:56:10');
  -- Result: 75
 
  -- Time only
  SELECT DATEDIFF(second, '08:54:55', '08:56:10');
  -- Result: 75

In PostgreSQL, you can use expressions to define the number of minutes (see above), multiply by 60 and by seconds.

PostgreSQL

-- Difference between Dec 30, 2011 08:54:55 and  Dec 30, 2011 08:56:10 in seconds
  SELECT ((DATE_PART('day', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp) * 24 +
                DATE_PART('hour', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp)) * 60 +
                DATE_PART('minute', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp)) * 60 +
                DATE_PART('second', '2011-12-30 08:56:10'::timestamp - '2011-12-30 08:54:55'::timestamp);
  -- Result: 75
 
  -- Time only
  SELECT (DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 +
               DATE_PART('minute', '08:56:10'::time - '08:54:55'::time)) * 60 +
               DATE_PART('second', '08:56:10'::time - '08:54:55'::time);
  -- Result: 75

PostgreSQL DATEDIFF - User-defined Function (UDF)

In addition to using separate expressions to calculate the date-time difference for each time unit, you can also use functions similar to the SQL Server DATEDIFF function.

PostgreSQL

CREATE OR REPLACE FUNCTION DateDiff (units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
     RETURNS INT AS $$
   DECLARE
     diff_interval INTERVAL;
     diff INT = 0;
     years_diff INT = 0;
   BEGIN
     IF units IN ('yy', 'yyyy', 'year', 'mm', 'm', 'month') THEN
       years_diff = DATE_PART('year', end_t) - DATE_PART('year', start_t);
 
       IF units IN ('yy', 'yyyy', 'year') THEN
         -- SQL Server does not count full years passed (only difference between year parts)
         RETURN years_diff;
       ELSE
         -- If end month is less than start month it will subtracted
         RETURN years_diff * 12 + (DATE_PART('month', end_t) - DATE_PART('month', start_t));
       END IF;
     END IF;
 
     -- Minus operator returns interval 'DDD days HH:MI:SS'
     diff_interval = end_t - start_t;
 
     diff = diff + DATE_PART('day', diff_interval);
 
     IF units IN ('wk', 'ww', 'week') THEN
       diff = diff/7;
       RETURN diff;
     END IF;
 
     IF units IN ('dd', 'd', 'day') THEN
       RETURN diff;
     END IF;
 
     diff = diff * 24 + DATE_PART('hour', diff_interval);
 
     IF units IN ('hh', 'hour') THEN
        RETURN diff;
     END IF;
 
     diff = diff * 60 + DATE_PART('minute', diff_interval);
 
     IF units IN ('mi', 'n', 'minute') THEN
        RETURN diff;
     END IF;
 
     diff = diff * 60 + DATE_PART('second', diff_interval);
 
     RETURN diff;
   END;
   $$ LANGUAGE plpgsql;

How to use PostgreSQL DATEDIFF function

The syntax is similar to SQL Server DATEDIFF, but you must specify the time units (seconds, minutes, etc. and their abbreviations) as string literals in PostgreSQL, for example:

   -- Difference between Dec 30, 2011 08:54:55 and  Dec 30, 2011 08:56:10 in seconds
  SELECT DATEDIFF('second', '2011-12-30 08:54:55'::timestamp, '2011-12-30 08:56:10'::timestamp);
  -- Result: 75

The PostgreSQL DATEDIFF function is only available for TIME

You can have another function that only works for time data types. PostgreSQL supports overloaded functions with the same name but different parameter data types:

CREATE OR REPLACE FUNCTION DateDiff (units VARCHAR(30), start_t TIME, end_t TIME)
     RETURNS INT AS $$
   DECLARE
     diff_interval INTERVAL;
     diff INT = 0;
   BEGIN
     -- Minus operator for TIME returns interval 'HH:MI:SS'
     diff_interval = end_t - start_t;
 
     diff = DATE_PART('hour', diff_interval);
 
     IF units IN ('hh', 'hour') THEN
       RETURN diff;
     END IF;
 
     diff = diff * 60 + DATE_PART('minute', diff_interval);
 
     IF units IN ('mi', 'n', 'minute') THEN
        RETURN diff;
     END IF;
 
     diff = diff * 60 + DATE_PART('second', diff_interval);
 
     RETURN diff;
   END;
   $$ LANGUAGE plpgsql;

Copy after login

For example, this function can be called as:

  -- Difference between 08:54:55 and 08:56:10 in seconds
  SELECT DATEDIFF('second', '08:54:55'::time, '08:56:10'::time);
  -- Result: 75

Summarize

This is all about this article about PostgreSQL date and time difference DATEDIFF. For more related PostgreSQL date and time difference DATEDIFF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!