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.