How to Store Dates and Times in a Database
A database field should reflect what a value means: a calendar date, an exact instant, or a local schedule with regional rules.
Start with the meaning
A birthday or holiday is usually a calendar date. A purchase or log event happened at an instant. A weekly meeting at 09:00 in London is a local schedule. These values may look similar on a screen, but they have different storage requirements and should not automatically be converted to the same representation.
A date-only type can preserve a calendar day without inventing midnight or a time zone. For an event that has already occurred, store an instant with a clearly documented UTC or offset convention. For a future local appointment, preserve the date, clock time and named zone so the intended hour remains understandable if rules change.
Understand your database type
Timestamp type names are not universal. Some databases distinguish values with and without time-zone semantics, but the exact behavior depends on the database and driver. A type that converts an input to an instant does not necessarily retain the original IANA zone name or the original offset string.
Read the documentation for your database, driver and application framework together. Check how parameters are written, how values are returned and whether the session time zone affects display. If the original zone matters, store it separately instead of assuming it survives an instant conversion.
Units, precision and ordering
If you store an epoch value as a number, state the epoch, unit and time scale in the schema or field documentation. Seconds, milliseconds and microseconds are not interchangeable. An integer can preserve a defined precision without the rounding issues that may occur when floating-point values are used.
A timestamp alone may not completely order simultaneous events. Clock skew, clock adjustments and limited precision can produce equal or misleading values across machines. For systems that need a reliable order, use a suitable sequence or ordering mechanism in addition to recording the event time. See timestamp precision.
Query date ranges safely
A half-open range includes its start and excludes its end. For example, events in a UTC day can be queried from the start of that day up to, but not including, the next day's start. Adjacent ranges then meet without overlap, and there is no need to invent a final millisecond.
For a local calendar day, first determine the two local midnight boundaries in the relevant named zone, then convert both to instants. Do not assume the span is always 24 hours: daylight-saving changes can make a local day shorter or longer. Read inclusive and exclusive ranges for the distinction.
Test before migrating
Test a migration with date-only fields, explicit UTC values, nonzero offsets, fractional seconds and values around daylight-saving transitions. Compare the stored value and the value returned by each application driver. Also check supported date ranges and integer widths in older systems.
Keep source context where it matters: original input, zone identifier or database version may be useful for auditing. Use the timestamp converter to inspect a sample, and see time zones and ISO 8601 for the underlying concepts.
PostgreSQL, as a concrete example
PostgreSQL has three types that cover most needs. date stores a calendar day. timestamp (without time zone) stores a wall-clock reading exactly as given. timestamptz stores an instant. It converts input to UTC and shows it in the session's TimeZone:
SET TIME ZONE 'Asia/Kolkata';
SELECT TIMESTAMPTZ '2026-12-05 08:00:00+00';
-- 2026-12-05 13:30:00+05:30
Despite the name, timestamptz does not keep the original offset or zone name. If you need to know which zone a user picked, store it in a separate column such as event_zone text holding Europe/Berlin.
Future appointments need the local time
For an appointment next year, keep the local date-time and the zone name as the source of truth, and calculate the instant from them. If a government changes its daylight-saving rules in the meantime, the stored UTC value would be an hour out, but the local time plus zone can be recalculated with updated tz data.
Other engines in brief
MySQL's TIMESTAMP converts values to UTC for storage using the session time zone and is limited to 2038. DATETIME stores the value as written, with no zone. SQLite has no dedicated date type. Values are kept as ISO 8601 text, a Julian day number or Unix seconds, and the date functions work on any of those. Whatever the engine, query ranges as half-open: >= start AND < next_start.
Common questions
Should the database server run in UTC?
It is a sensible default and makes logs easier to read, but the column types matter more. A timestamptz value is the same instant whatever the server's zone, while a plain timestamp means whatever the application assumed when it wrote it.
How should I store a duration?
Use an integer count of a fixed unit, such as seconds or milliseconds, or an interval type if the database has one. Do not store durations as timestamps.