How to get current date time in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Getting the current date and time in MySQL is simple at syntax level, but production correctness depends on timezone handling, column types, and query context. Choosing between NOW(), CURRENT_DATE(), and UTC-based functions affects data consistency across services. This guide explains practical patterns you can run directly.
Core Current-Time Functions
MySQL provides several built-in functions with different granularity.
NOW() and CURRENT_TIMESTAMP() are equivalent in common usage. Use CURRENT_DATE() when you need only date logic, such as daily partitions.
For fractional seconds:
This is useful for high-frequency event ordering.
Session Timezone vs UTC
NOW() uses the session timezone. If application servers run in multiple regions, this can create inconsistent records unless you standardize.
Check settings:
Set session timezone explicitly:
Or convert at query time:
In distributed systems, storing UTC in TIMESTAMP or normalized DATETIME fields is usually safest.
Inserting Current Time into Tables
Typical insert patterns:
You can also define defaults in schema so inserts remain consistent across clients:
This pattern is reliable for change tracking without requiring application-side timestamp population.
Filtering by Current Date Safely
A frequent mistake is wrapping indexed columns in date functions, which can disable index usage. Prefer range predicates.
Less efficient pattern:
Index-friendly pattern:
The range approach scales better on large tables.
Choosing TIMESTAMP vs DATETIME
Use TIMESTAMP when you want MySQL to manage timezone conversion relative to session settings. Use DATETIME when the stored value should remain literal and unaffected by timezone conversion.
Guideline:
- event logs and operational records: usually
TIMESTAMP - business-local schedule values: often
DATETIME
Be explicit in schema docs so teams apply one policy consistently.
Application Integration Example
A Python client inserting current time from SQL side avoids clock skew between app nodes.
Using server-side NOW() keeps ordering and audit behavior consistent.
Formatting Current Time for Output
Keep storage and display concerns separate. Store high-fidelity timestamp values, then format at query time only for user-facing output.
This pattern preserves accurate raw values while giving predictable formatted strings for reports.
Common Pitfalls
- Assuming
NOW()is always UTC without checking session timezone. - Mixing
DATETIMEandTIMESTAMPwithout a documented convention. - Wrapping indexed datetime columns in functions during filtering.
- Relying on application node clocks for audit-critical timestamps.
- Forgetting microsecond precision when events occur very close together.
Summary
- Use
NOW()orCURRENT_TIMESTAMP()for current datetime values. - Standardize timezone policy, commonly UTC, across sessions and services.
- Prefer schema defaults for
created_atandupdated_atconsistency. - Use range predicates for date filtering to preserve index performance.
- Choose
TIMESTAMPorDATETIMEbased on conversion semantics, not habit.
Related reading
- how to get data from dynamodb using rest api
- How to get database structure in MySQL via query?
- How to get dynamodb to only return certain columns
- How to get ER model of database from server with Workbench
- How to get ID of the last updated row in MySQL?
- How to get input file name as column in AWS Athena external tables
- How to get kafka message's headers in Kafka Connect Sink connector with MongoDB
- How to get last insert id after insert query inside of a transaction in CodeIgniter

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.