SQL · Module 2 · Lesson 31 min read

Date & Time Types

The main types

TypeExampleUse for
DATE2026-11-24birthdays, due dates, expense dates
TIME14:30:00opening hours
TIMESTAMP2026-11-24 14:30:00⚠️ no time zone
TIMESTAMPTZ2026-11-24 14:30:00+05:45events in time ✅
INTERVAL3 days, 2 hours 30 minutesdurations

TIMESTAMPTZ vs TIMESTAMP

  • TIMESTAMPTZ stores an exact moment (internally in UTC) and displays it in the session's time zone ✅ use it for created_at, paid_at, logins
  • TIMESTAMP ("without time zone") is just a calendar date + clock time with no zone. It's ambiguous when users are in different countries.

Input format

Always use ISO 8601: '2026-11-24', '2026-11-24 14:30:00+05:45'. Avoid '24/11/2026' (is it day/month or month/day?).

Current date & time

  • now() / CURRENT_TIMESTAMP – the current moment (same value for the whole transaction)
  • CURRENT_DATE – today's date
  • CURRENT_TIME

Date arithmetic

  • DATE + INTEGER → adds days: CURRENT_DATE + 7
  • DATE - DATE → number of days (an integer)
  • TIMESTAMPTZ + INTERVAL → now() + INTERVAL '2 hours'
  • TIMESTAMPTZ - TIMESTAMPTZ → an interval

Time zones

  • SET timezone = 'Asia/Kathmandu'; changes how values are displayed for this session
  • ts AT TIME ZONE 'Asia/Kolkata' converts for display
  • Store in TIMESTAMPTZ; convert to the user's zone only when showing it

(Date functions like EXTRACT, DATE_TRUNC and TO_CHAR are in Unit 5.)

Example

SQL
SELECT CURRENT_DATE                     AS today,
       now()                            AS this_moment,
       CURRENT_DATE + 7                 AS next_week,
       now() + INTERVAL '2 hours 30 minutes' AS later;

-- Days between two dates (an integer)
SELECT DATE '2026-12-31' - DATE '2026-11-24' AS days_left;   -- 37

-- Bills and how many days until they are due (from 24 Nov 2026)
SELECT title, due_date, due_date - DATE '2026-11-24' AS days_until_due
FROM bills
WHERE NOT paid
ORDER BY due_date;

-- TIMESTAMPTZ shows the same moment in different zones
SET timezone = 'UTC';
SELECT TIMESTAMPTZ '2026-11-24 10:00:00+00' AS utc_time;
SET timezone = 'Asia/Kathmandu';
SELECT TIMESTAMPTZ '2026-11-24 10:00:00+00' AS kathmandu_time;  -- 15:45:00+05:45

RESET timezone;

-- How many days has each user been a member? (as of 24 Nov 2026)
SELECT name, DATE '2026-11-24' - created_at::date AS days_as_member
FROM users
ORDER BY created_at
LIMIT 3;
--  Asha Sharma | 318
--  Ravi Thapa  | 283
--  Sita Gurung | 264