How to get today and yesterday in PosgreSQL

Dates are always a pain to manage, so here are my 2 cents for PostgreSQL.

The official documentation is here.

Getting the current date and time

SELECT NOW()

Getting the current day, no time

SELECT date_trunc('day', NOW())::date;

One hour later

SELECT (NOW() + interval '1 hour') AS one_hour_later;

One day later

SELECT NOW()+1

The day before

SELECT NOW()-1

For more complex calculations you can use intervals

For example:

SELECT (NOW() + interval '1 day') AS now_but_tomorrow;
comments powered by Disqus