SQL: Structured Query LanguageSQL FundamentalsDate and Timestamp Types
What is the difference between DATE and TIMESTAMP?
Question 13 of 30 · Updated
On this page
- 1. DATE vs TIMESTAMP at a glance
- 2. Choosing types when defining a table
- 3. Converting between DATE and TIMESTAMP
- 4. Date arithmetic and timestamp arithmetic
- 5. TIMESTAMP WITHOUT TIME ZONE vs TIMESTAMPTZ
- 6. Filtering timestamp values for a day
- 7. Storage and performance
- 8. Common mistakes and selection guidelines
- 9. Interview-ready answer
- Validation and references
DATE represents a calendar day without a time of day. TIMESTAMP represents a date together with a time of day, potentially including fractional seconds. In PostgreSQL, TIMESTAMP without qualification means TIMESTAMP WITHOUT TIME ZONE. TIMESTAMP WITH TIME ZONE (also called TIMESTAMPTZ) represents an instant that can be displayed in different time zones.
For example, a birth date of 15 January 2026 requires only a DATE, while an audit event occurring at 14:30:45 on that day needs a timestamp. For an event recorded across systems in multiple time zones, TIMESTAMPTZ is generally the appropriate PostgreSQL type.
1. DATE vs TIMESTAMP at a glance#
| Property | DATE | TIMESTAMP WITHOUT TIME ZONE | TIMESTAMP WITH TIME ZONE |
|---|---|---|---|
| Meaning | Calendar day | Local date and clock time | Instant on the timeline |
| Example input | 2026-01-15 | 2026-01-15 14:30:45 | 2026-01-15 14:30:45+00 |
| Time of day | No | Yes | Yes, displayed in a time zone |
| Fractional seconds | No | Up to microsecond precision in PostgreSQL | Up to microsecond precision in PostgreSQL |
| PostgreSQL value size | 4 bytes | 8 bytes | 8 bytes |
| Typical use | Birthdays, holidays, due dates | Wall-clock appointments with separately known zone | Creation times, payments, logs |
The examples and sizes in this lesson refer to PostgreSQL. SQL products differ in timestamp names, supported precision, storage, range and time-zone rules. In particular, SQL Server's historical timestamp type is a synonym for rowversion, not a date-and-time type; SQL Server applications commonly use datetime2 or datetimeoffset for temporal values.
2. Choosing types when defining a table#
Suppose an application schedules appointments and records when they are created. A possible PostgreSQL schema is:
SQLCREATE TABLE appointments ( appointment_id BIGINT PRIMARY KEY, service_day DATE NOT NULL, scheduled_local TIMESTAMP NOT NULL, scheduled_zone TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP );
service_day represents a calendar day. scheduled_local represents the intended wall-clock appointment time, and scheduled_zone is intended to hold a named time zone such as Asia/Kolkata. created_at records an instant. If the appointment itself must represent an unambiguous instant, store a TIMESTAMPTZ value too, or use it as the primary scheduled-time representation. Keep the original zone or scheduling intent separately when that information matters.
The example is illustrative DDL, not a complete production scheduling model. It does not validate the zone name or ensure that the day agrees with the appointment. Daylight-saving transitions can make a local time ambiguous or nonexistent. An application must define a policy for resolving those situations. No teaching tables were created in the publishing database.
3. Converting between DATE and TIMESTAMP#
PostgreSQL supports typed literals and CAST. The following read-only statement demonstrates both types and conversions:
SQLSELECT DATE '2026-01-15' AS calendar_day, TIMESTAMP '2026-01-15 14:30:45.123456' AS local_datetime, CAST(TIMESTAMP '2026-01-15 14:30:45' AS date) AS date_only, CAST(DATE '2026-01-15' AS timestamp) AS midnight;
| calendar_day | local_datetime | date_only | midnight |
|---|---|---|---|
| 2026-01-15 | 2026-01-15 14:30:45.123456 | 2026-01-15 | 2026-01-15 00:00:00 |
Converting a timestamp without time zone to DATE removes its time-of-day component. Converting a DATE to a timestamp without time zone introduces midnight as the clock time; it does not recover any previously discarded time. A date is not inherently an instant at midnight UTC. To derive an instant from a calendar day and clock time, the intended time zone must also be known.
TIMESTAMP(p) permits specifying fractional-second precision from 0 through 6 in PostgreSQL. Lower precision can round fractional seconds. The default supports microseconds. Do not confuse precision with clock accuracy: the type's ability to represent a fractional second does not prove that the source clock is accurate to that precision.
4. Date arithmetic and timestamp arithmetic#
Calendar calculations and elapsed-time calculations answer different questions. PostgreSQL supports adding an integer number of days to a DATE and adding an INTERVAL to a timestamp.
SQLSELECT DATE '2026-01-15' + 7 AS next_week, DATE '2026-01-22' - DATE '2026-01-15' AS days_between, TIMESTAMP '2026-01-15 14:30:00' + INTERVAL '90 minutes' AS later_time, TIMESTAMP '2026-01-15 16:00:00' - TIMESTAMP '2026-01-15 14:30:00' AS elapsed;
| next_week | days_between | later_time | elapsed |
|---|---|---|---|
| 2026-01-22 | 7 | 2026-01-15 16:00:00 | 01:30:00 |
The difference between two DATE values in PostgreSQL is an integer number of days. Subtracting timestamps produces an interval. For local timestamps, this arithmetic operates on wall-clock values, not necessarily actual elapsed time between instants. For elapsed duration across time-zone or daylight-saving changes, use properly resolved instants.
5. TIMESTAMP WITHOUT TIME ZONE vs TIMESTAMPTZ#
A timestamp without time zone stores a date and clock time without specifying where that clock time applies. The same local value may refer to different instants in different places. A TIMESTAMPTZ value represents an instant; PostgreSQL converts timezone-qualified input to its internal UTC-based representation and displays the instant according to the session time zone. It does not retain the original named zone or original offset.
The following Mermaid diagram illustrates why the date of an event can depend on the viewing time zone.
Both branches describe the same instant. The local calendar date is 15 January in UTC but 16 January in Kolkata. The diagram uses an explicit example with a known offset, not a claim that all time-zone offsets are constant throughout the year.
SQLSELECT (TIMESTAMPTZ '2026-01-15 09:00:00+05:30' = TIMESTAMPTZ '2026-01-15 03:30:00+00') AS same_instant, TIMESTAMPTZ '2026-01-15 20:00:00+00' AT TIME ZONE 'Asia/Kolkata' AS local_time, (TIMESTAMPTZ '2026-01-15 20:00:00+00' AT TIME ZONE 'Asia/Kolkata')::date AS local_day;
| same_instant | local_time | local_day |
|---|---|---|
| true | 2026-01-16 01:30:00 | 2026-01-16 |
The first expression compares two representations of the same instant. The second converts an instant to the local timestamp in the named zone, and the third extracts its local date. Casting a TIMESTAMPTZ directly to DATE instead uses the session time zone; explicitly selecting the intended zone makes business-day calculations more predictable. Supabase documentation recommends retaining the default UTC database time-zone configuration. This does not eliminate the need to interpret calendar dates in the user's intended zone.
6. Filtering timestamp values for a day#
An equality comparison against midnight only matches that timestamp, not every event on the same day. A half-open interval includes the beginning of the day and excludes the beginning of the following day. Here is a simple example with local timestamps:
SQLWITH events(id, occurred_at) AS ( VALUES (1, TIMESTAMP '2026-01-15 00:00:00'), (2, TIMESTAMP '2026-01-15 23:59:59.999999'), (3, TIMESTAMP '2026-01-16 00:00:00') ) SELECT id FROM events WHERE occurred_at >= TIMESTAMP '2026-01-15 00:00:00' AND occurred_at < TIMESTAMP '2026-01-16 00:00:00' ORDER BY id;
| id | |---:| | 1 | | 2 |
The final event belongs to the following day and is excluded. This avoids hard-coding a last instant such as 23:59:59, which may miss fractional-second values. A predicate on the original timestamp column can also allow a suitable ordinary index to be considered; index use depends on the actual plan. For TIMESTAMPTZ business-day filtering, derive both boundaries from the calendar dates in the intended named zone. A local day may not contain exactly 24 elapsed hours when daylight-saving transitions occur. The complete day-filtering and indexing strategy is a separate topic.
7. Storage and performance#
PostgreSQL provides pg_column_size to inspect value size:
SQLSELECT pg_column_size(DATE '2026-01-15') AS date_bytes, pg_column_size(TIMESTAMP '2026-01-15 14:30:00') AS timestamp_bytes, pg_column_size(TIMESTAMPTZ '2026-01-15 14:30:00+00') AS timestamptz_bytes;
| date_bytes | timestamp_bytes | timestamptz_bytes |
|---|---|---|
| 4 | 8 | 8 |
These measurements are value sizes, not complete row or index entry sizes. Use DATE when time information is unnecessary because it models the requirement accurately, not merely to save a few bytes. Avoid choosing DATE for audit events if the system needs to reconstruct their ordering within a day. Even timestamps may tie, so a deterministic sort sometimes needs a separate unique tie-breaker.
8. Common mistakes and selection guidelines#
| Requirement | Suggested PostgreSQL type |
|---|---|
| Date of birth or holiday | DATE |
| Invoice due day | DATE if payment is due by a business calendar day |
| Wall-clock appointment whose zone is separately known | TIMESTAMP plus zone information and a resolution policy |
| Account creation, payment confirmation or audit event | TIMESTAMPTZ |
| Duration such as 90 minutes | INTERVAL, or a numeric duration with an explicit unit |
Avoid storing dates as arbitrary strings merely to control their display. Format temporal values for presentation in the client; use typed values for filtering and arithmetic. Do not assume a timestamp without time zone is automatically UTC, or that TIMESTAMPTZ remembers the original zone. If a deadline is an exact instant rather than a calendar day, choose an instant representation. Be explicit about input formats and time-zone assumptions, especially when migrating legacy timestamp columns.
9. Interview-ready answer#
DATE stores only a calendar date, while TIMESTAMP stores both a date and a time of day, optionally with fractional-second precision. DATE is suitable for birthdays, holidays and due dates that do not require a clock time. In PostgreSQL, TIMESTAMP WITHOUT TIME ZONE represents a local date and time, while TIMESTAMPTZ represents an instant and displays it according to the session time zone. TIMESTAMPTZ is normally preferable for event logs and creation times across systems. Converting a timestamp to a date discards time information, and deriving a date from an instant requires the appropriate time zone. Exact type semantics vary across SQL products.
Validation and references#
All five read-only PostgreSQL example statements were executed against synthetic values: date and timestamp conversions, arithmetic, time-zone conversion and instant equality, half-open range filtering, and storage-size inspection. The illustrative CREATE TABLE definition was not executed. No educational DDL or application data changes were made.
The Mermaid diagram is stored as a fenced mermaid code block with actual line breaks. A structural syntax check for its simple flowchart nodes and edges passed, and the semantics were reviewed against the tested example. An automated Mermaid parser and the public website renderer were unavailable, so live rendering is unverified.
Documentation consulted through the Supabase documentation connector:
- Supabase — Database configuration: default UTC configuration and time-zone guidance.
Additional authoritative reading (not live-verified in this session):
- PostgreSQL — Date/Time Types.
- PostgreSQL — Date/Time Functions and Operators.
- PostgreSQL — Database Object Size Functions.
- Microsoft — rowversion (Transact-SQL).
Live web browsing was unavailable. These additional references are suggested reading, not independently consulted sources.
References
- 1.Supabase — Database configurationsupabase.com
- 2.PostgreSQL — Date/Time Types (not live-verified)postgresql.org
- 3.
- 4.
- 5.Microsoft — rowversion (not live-verified)learn.microsoft.com
Related questions
Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning