Note CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP are ANSI standard and work everywhere. NOW() is a function that does the same as CURRENT_TIMESTAMP but is not standard SQL. In a transaction, these return the time the transaction started, not the current wall clock time.
current datetodaynowcurrent timestampget current time
Date Addition / Subtraction
Syntax
-- PostgreSQLdate+ INTERVAL 'n unit'-- MySQLDATE_ADD(date, INTERVAL n unit)-- ANSIdate+ INTERVAL 'n' unit
Note Interval units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND. Adding months is tricky - Jan 31 + 1 month may yield Feb 28 or Mar 3 depending on the database. PostgreSQL truncates to end of month; MySQL may overflow.
Note PostgreSQL subtracts dates directly. MySQL uses DATEDIFF(end, start) - note the argument order is end first. SQL Server's DATEDIFF takes a unit argument: DATEDIFF(DAY, start, end). Always check argument order in your database.
date differencedays betweendatediffsubtract datestime elapsed
SELECT
order_date,EXTRACT(YEAR FROM order_date)AS order_year,EXTRACT(MONTH FROM order_date)AS order_month,EXTRACT(DOW FROM order_date)AS day_of_week
FROM orders;
Note EXTRACT is ANSI standard. In PostgreSQL, DOW is 0 (Sunday) to 6 (Saturday). MySQL's DAYOFWEEK returns 1 (Sunday) to 7 (Saturday). EXTRACT(EPOCH FROM timestamp) gives Unix timestamp in PostgreSQL.
extract yearextract monthdate partget year from dateday of week
-- PostgreSQLSELECTTO_CHAR(order_date,'YYYY-MM-DD')AS iso_date,TO_CHAR(order_date,'Mon DD, YYYY')AS pretty_date
FROM orders;-- MySQLSELECTDATE_FORMAT(order_date,'%Y-%m-%d')AS iso_date,DATE_FORMAT(order_date,'%b %d, %Y')AS pretty_date
FROM orders;
Output
-- iso_date | pretty_date
-- 2025-10-15 | Oct 15, 2025
Note Format codes differ between databases. PostgreSQL uses YYYY, MM, DD, HH24, MI, SS. MySQL uses %Y, %m, %d, %H, %i, %s. Format dates in the application layer when possible to avoid DB-specific code.
-- PostgreSQLDATE_TRUNC('unit',timestamp)-- MySQLDATE(timestamp)-- truncate to dateDATE_FORMAT(timestamp,'%Y-%m-01')-- truncate to month
Example
-- PostgreSQL: group orders by monthSELECTDATE_TRUNC('month', order_date)AS order_month,COUNT(*)AS order_count,SUM(total_amount)AS revenue
FROM orders
GROUPBYDATE_TRUNC('month', order_date)ORDERBY order_month;
Note DATE_TRUNC is extremely useful for time-series grouping. It rounds down to the start of the given period (year, quarter, month, week, day, hour). MySQL lacks DATE_TRUNC - use DATE_FORMAT or manual rounding.
truncate dateround dategroup by monthgroup by weekdate_trunc
INTERVAL Arithmetic
Syntax
INTERVAL 'quantity unit'-- Can combine: INTERVAL '2 hours 30 minutes'
Example
SELECT
created_at,
created_at + INTERVAL '7 days'AS expires_at,NOW()- created_at AS age
FROM sessions
WHERE created_at >NOW()- INTERVAL '24 hours';
Output
-- Sessions created in the last 24 hours, with expiry and age
Note PostgreSQL supports rich interval syntax: '1 year 2 months 3 days'. MySQL intervals are single-unit: INTERVAL 1 YEAR, INTERVAL 30 DAY. Subtracting two timestamps gives an interval in PostgreSQL but not in MySQL.
intervaltime intervaldate intervalhours agodays from now
AGE Function (PostgreSQL)
Syntax
AGE(timestamp1, timestamp2)AGE(timestamp)-- shorthand for AGE(NOW(), timestamp)
Example
SELECT
first_name,
birth_date,AGE(birth_date)AS age,EXTRACT(YEAR FROMAGE(birth_date))AS years_old
FROM users;
Output
-- first_name | birth_date | age | years_old
-- Alice | 1990-05-15 | 35 years 6 mons 5 days | 35
Note AGE is PostgreSQL-specific. It returns a human-readable interval. MySQL has no direct equivalent - compute age manually with TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) and adjust for whether the birthday has passed this year.
calculate ageage from dateyears between datesage function
Timezone Handling
Syntax
-- PostgreSQLtimestamp AT TIME ZONE 'zone'-- MySQLCONVERT_TZ(datetime, from_tz, to_tz)
Example
-- PostgreSQLSELECT
created_at AT TIME ZONE 'UTC'AS utc_time,
created_at AT TIME ZONE 'America/New_York'AS eastern_time
FROM events;-- MySQLSELECTCONVERT_TZ(created_at,'+00:00','-05:00')AS eastern_time
FROM events;
Note Always store timestamps in UTC (use TIMESTAMPTZ in PostgreSQL). Convert to local time only for display. Daylight saving time offsets change - use named zones ('America/New_York') instead of fixed offsets ('-05:00') when possible.
timezoneconvert timezoneutcat time zonetime zone conversion