Dates and Times

Updated · 8 min read
mediumPostgreSQL 18sqldatestimestamptime-zonedaylight-savinginterview

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

  • DATE is a calendar day; TIME a time of day; TIMESTAMP a date and time with no zone; TIMESTAMPTZ a 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

SQL▶ Try it
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;
Output
   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.

TypeHoldsFee Tracker use
DATEA calendar daypaid_on, due_on, joined_on
TIMEA time of dayClass start times
TIMESTAMPDate and time, no zoneRarely the right choice
TIMESTAMPTZAn exact momentWhen 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:

SQL▶ Try it
SET DateStyle = 'ISO, DMY';
SELECT '01/05/2026'::date AS read_day_first;
Output
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?

SQL▶ Try it
SELECT payment_id, due_on, paid_on, paid_on - due_on AS days_late
FROM payments
ORDER BY payment_id;
Output
 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.

paid_on minus due_on for each payment: negative is early, positive is late

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?

SQL▶ Try it
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;
Output
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:

SQL▶ Try it
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;
Output
 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)
6:00 PM in Dallas is 4:00 PM in San Francisco and midnight UTC

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:

SQL▶ Try it
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;
Output
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.

Across the March 8 clock change, noon plus 1 day is noon; noon plus 24 hours is 1:00 PM

Date Types in Other Databases

PostgreSQLMySQLSQL ServerOracle
DATEDATEDATEDATE (also stores a time)
TIMESTAMPDATETIMEDATETIME2TIMESTAMP
TIMESTAMPTZTIMESTAMP (stored as UTC)DATETIMEOFFSETTIMESTAMP 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
SQL▶ Try it
SELECT first_name, joined_on, DATE '2026-01-05' - joined_on AS days_as_member
FROM students
ORDER BY student_id;
Output
 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.

Quick check: Dates and times

3 questions
1What does DATE '2025-12-19' - DATE '2025-12-15' return?
2A reminder is sent at 6:00 PM Dallas time. What time is it in San Francisco?
3Why write '2026-01-05' instead of '01/05/2026'?

Key Takeaways

You want toWrite
A date literalDATE '2026-01-05'
Days between datespaid_on - due_on
A moment on San Francisco's clockts AT TIME ZONE 'America/Los_Angeles'
Set the session zoneSET TIME ZONE 'America/Chicago';

Was this tutorial helpful?

Lesson 53 of 115