postgresqldatetimedate-arithmetic

How to get the number of days in a month?


I am trying to get the following in Postgres:

select day_in_month(2);

Expected output:

28

Is there any built-in way in Postgres to do that?


Solution

  • SELECT  
        DATE_PART('days', 
            DATE_TRUNC('month', NOW()) 
            + '1 MONTH'::INTERVAL 
            - '1 DAY'::INTERVAL
        )
    

    Substitute NOW() with any other date.