Dates and Times
Dates and times are their own data types, with their own rules: a date is a calendar day, a timestamp is a moment, and a time zone decides what clock that moment shows. The Fee Tracker's students live two hours apart: Navika in Dallas (Central time), Srihitha and Advitha in San Francisco (Pacific time). "Due at 6:00 PM" means two different moments for them. This lesson covers the four date and time types, how to write them so no database misreads them, subtracting dates, converting between time zones, and a daylight-saving surprise. Date Functions, next, covers extracting parts and formatting.
In One Minute
DATEis a calendar day;TIMEa time of day;TIMESTAMPa date and time with no zone;TIMESTAMPTZa moment, shown in your session's zone.- Write literals in ISO form:
DATE '2026-01-05',TIMESTAMPTZ '2026-01-05 18:00-06'. - Subtracting two dates gives a whole number of days:
paid_on - due_on. moment AT TIME ZONE 'America/Los_Angeles'shows a moment on another region's clock.- Use region names like
America/Chicago, never fixed offsets, so daylight saving is handled for you.
The Four Types
SELECT DATE '2026-01-05' AS a_date,
TIME '18:00' AS a_time,
TIMESTAMP '2026-01-05 18:00' AS a_timestamp,
TIMESTAMPTZ '2026-01-05 18:00-06' AS a_moment; a_date | a_time | a_timestamp | a_moment
------------+----------+---------------------+------------------------
2026-01-05 | 18:00:00 | 2026-01-05 18:00:00 | 2026-01-06 00:00:00+00
(1 row)The first three came back exactly as written. The TIMESTAMPTZ was written as 6:00 PM at UTC−6 (Dallas in winter), but Try it's session runs in UTC, so PostgreSQL shows the same moment on the UTC clock: midnight, January 6. A TIMESTAMPTZ stores the moment, not the digits you typed. SQL Data Types showed this with Navika's study log.
| Type | Holds | Fee Tracker use |
|---|---|---|
DATE | A calendar day | paid_on, due_on, joined_on |
TIME | A time of day | Class start times |
TIMESTAMP | Date and time, no zone | Rarely the right choice |
TIMESTAMPTZ | An exact moment | When a payment was received |
Write Dates in ISO Form
'YYYY-MM-DD' means the same thing everywhere; '01/05/2026' doesn't. PostgreSQL reads a slashed date according to its DateStyle setting. With the US default it's January 5; with day-first settings it's May 1:
SET DateStyle = 'ISO, DMY';
SELECT '01/05/2026'::date AS read_day_first;SET
read_day_first
----------------
2026-05-01
(1 row)Same text, a different day, four months apart. Write '2026-01-05', and PostgreSQL, MySQL, Oracle and SQLite read it the same way under any setting. (SQL Server's older datetime type can misread it under some language settings; '20260105' is safe there, and its date type reads both correctly.) Impossible dates are refused, too: '2026-02-30'::date fails with date/time field value out of range.
Date Arithmetic
Subtracting two dates gives the number of days between them, as an integer. How many days early or late was each payment?
SELECT payment_id, due_on, paid_on, paid_on - due_on AS days_late
FROM payments
ORDER BY payment_id; payment_id | due_on | paid_on | days_late
------------+------------+------------+-----------
1 | 2025-09-01 | 2025-08-28 | -4
2 | 2025-11-01 | 2025-10-30 | -2
3 | 2025-12-01 | 2025-12-03 | 2
4 | 2026-01-02 | 2025-12-29 | -4
5 | 2026-01-05 | 2026-01-05 | 0
6 | 2025-11-15 | 2025-11-14 | -1
7 | 2025-12-15 | 2025-12-19 | 4
(7 rows)Negative numbers mean early. Payment 3 was 2 days late and payment 7 was 4 days late. Adding a number to a date moves it by that many days: DATE '2026-01-05' + 30 is February 4. Date Functions covers intervals like "1 month", which don't have a fixed number of days.
Time Zones
AT TIME ZONE shows a moment on a particular region's clock. A payment reminder goes out at 6:00 PM Dallas time. What time is that for each sister?
SET TIME ZONE 'America/Chicago';
SELECT TIMESTAMPTZ '2026-01-05 18:00' AS sent_dallas_time,
TIMESTAMPTZ '2026-01-05 18:00' AT TIME ZONE 'America/Los_Angeles' AS san_francisco_clock,
TIMESTAMPTZ '2026-01-05 18:00' AT TIME ZONE 'UTC' AS utc_clock;SET
sent_dallas_time | san_francisco_clock | utc_clock
------------------------+---------------------+---------------------
2026-01-05 18:00:00-06 | 2026-01-05 16:00:00 | 2026-01-06 00:00:00
(1 row)One moment, three clocks: 6:00 PM in Dallas is 4:00 PM in San Francisco and midnight UTC. SET TIME ZONE sets the session's zone, which decides how TIMESTAMPTZ values are written and read. The Fee Tracker stores each city's zone in cities.time_zone, so a query can show every student their own local time:
SELECT s.first_name, c.time_zone,
TIMESTAMPTZ '2026-01-06 00:00:00+00' AT TIME ZONE c.time_zone AS local_clock
FROM students AS s
JOIN cities AS c ON c.city = s.city AND c.state = s.state
ORDER BY s.student_id; first_name | time_zone | local_clock
------------+---------------------+---------------------
Srihitha | America/Los_Angeles | 2026-01-05 16:00:00
Advitha | America/Los_Angeles | 2026-01-05 16:00:00
Navika | America/Chicago | 2026-01-05 18:00:00
(3 rows)Daylight Saving Time
On the day clocks change, "one day later" and "24 hours later" are different. In 2026, US clocks spring forward on March 8. In Dallas:
SET TIME ZONE 'America/Chicago';
SELECT TIMESTAMPTZ '2026-03-07 12:00' + INTERVAL '1 day' AS one_day_later,
TIMESTAMPTZ '2026-03-07 12:00' + INTERVAL '24 hours' AS twenty_four_hours_later;SET
one_day_later | twenty_four_hours_later
------------------------+-------------------------
2026-03-08 12:00:00-05 | 2026-03-08 13:00:00-05
(1 row)+ 1 day keeps the clock time, noon to noon, which is only 23 real hours across the change. + 24 hours adds exactly 24 hours, landing at 1:00 PM. The offset changed from −06 to −05. PostgreSQL knows the rules because the zone was named America/Chicago; a fixed offset like -06 knows nothing about daylight saving.
Date Types in Other Databases
| PostgreSQL | MySQL | SQL Server | Oracle |
|---|---|---|---|
DATE | DATE | DATE | DATE (also stores a time) |
TIMESTAMP | DATETIME | DATETIME2 | TIMESTAMP |
TIMESTAMPTZ | TIMESTAMP (stored as UTC) | DATETIMEOFFSET | TIMESTAMP WITH TIME ZONE |
paid_on - due_on (days) | DATEDIFF(paid_on, due_on) | DATEDIFF(day, due_on, paid_on) | paid_on - due_on |
Oracle's DATE includes a time of day, which surprises people moving from other databases. Tabs showing each database's version are coming to this course.
Common Mistakes
Slashed dates. '01/05/2026' can be January or May. Use '2026-01-05'.
TIMESTAMP for moments. Without a zone, 6:00 PM means nothing exact. Use TIMESTAMPTZ.
Fixed offsets instead of regions. -06 is wrong half the year in Dallas. Use America/Chicago.
Assuming a day is 24 hours. Across daylight saving changes it's 23 or 25.
Interview Corner
What's the difference between TIMESTAMP and TIMESTAMPTZ in PostgreSQL?
TIMESTAMP stores a date and time with no time zone. TIMESTAMPTZ stores an exact moment (internally in UTC) and displays it in the session's time zone.
How do you find the number of days between two dates in PostgreSQL?
Subtract them: paid_on - due_on returns an integer number of days. MySQL uses DATEDIFF(a, b); SQL Server uses DATEDIFF(day, b, a).
Why use time zone names like America/Chicago instead of offsets like -06?
Names carry daylight-saving rules, so the offset changes correctly through the year. A fixed offset is right only part of the year.
Practice
Challenge: Show each student's first name, joined_on, and how many days they've been a member as of 2026-01-05, sorted by student_id.
Show solution
SELECT first_name, joined_on, DATE '2026-01-05' - joined_on AS days_as_member
FROM students
ORDER BY student_id; first_name | joined_on | days_as_member
------------+------------+----------------
Srihitha | 2024-08-19 | 504
Advitha | 2025-01-13 | 357
Navika | 2025-09-02 | 125
(3 rows)Subtracting dates gives whole days. Using the fixed date 2026-01-05 (the Fee Tracker's "today") instead of current_date keeps the answer the same every time you run it.
Key Takeaways
| You want to | Write |
|---|---|
| A date literal | DATE '2026-01-05' |
| Days between dates | paid_on - due_on |
| A moment on San Francisco's clock | ts AT TIME ZONE 'America/Los_Angeles' |
| Set the session zone | SET TIME ZONE 'America/Chicago'; |
Was this tutorial helpful?