How to get the current date and time
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting the current date and time is easy in most languages, but the useful answer depends on what “current” means for your application. You usually need to decide between local time and UTC, and between a machine-friendly timestamp and a user-facing formatted string.
Start With the Distinction Between Time Sources
The basic operation is “read the system clock,” but there are two common output styles:
- a date-time object you can compute with
- a formatted string you can display
You should usually keep those separate. Work with date-time objects first, then format for users at the edge of the application.
Python Example
In Python, datetime.now() gives local time, while datetime.now(timezone.utc) gives UTC.
If the timestamp will be stored, logged, or exchanged across systems, UTC is often safer.
JavaScript Example
In JavaScript, new Date() captures the current moment.
The same underlying date can be displayed differently:
- '
toISOString()for a stable UTC string' - '
toLocaleString()for user-facing local display'
This is a good example of why “get the current date and time” is only half the question. The other half is “in what form?”
Java Example
In modern Java, prefer the java.time API.
Instant is ideal for machine time in UTC. ZonedDateTime is useful when you care about an actual time zone. LocalDateTime is convenient, but it has no zone information attached.
UTC Versus Local Time
A practical rule is:
- store and exchange timestamps in UTC
- display them in the user's local time zone when needed
This avoids ambiguity when applications run across servers, time zones, or daylight-saving changes.
If you use local time for persistence without storing the zone, timestamps can become hard to interpret later.
Formatting Should Happen Explicitly
Do not rely on default string formatting if the output matters for users or for APIs. Format intentionally.
Python:
JavaScript:
Java:
Formatting is a presentation decision. Keep it separate from how you store the actual time value.
Beware of Time Zone Assumptions
Many bugs come from assuming the server's local time zone is the same as the user's. That may be false in web applications, scheduled jobs, mobile systems, or cloud deployments.
If a timestamp matters across systems, make the zone explicit. UTC is usually the easiest baseline.
Timestamps for Logging and IDs
For logs and cross-service diagnostics, ISO 8601 UTC strings are usually the safest representation.
This is sortable, unambiguous, and consistent across environments.
For IDs or monotonic sequencing, however, a wall-clock timestamp may not be enough by itself. “Current time” is not the same thing as a guaranteed unique counter.
Common Pitfalls
A common mistake is storing local time without storing a time zone or offset.
Another mistake is formatting dates too early and then trying to compute with strings later.
Developers also often confuse “the current date and time” with “a user-friendly display string.” Those are different stages of the same data.
Finally, defaults differ across languages and environments. If correctness matters, do not rely on implicit time zone behavior.
Summary
- Read the current time as a date-time object first, then format it later if needed.
- Prefer UTC for storage, logs, and cross-system exchange.
- Use local formatting only for display.
- Choose APIs that make time zone information explicit when the application needs it.
- A correct current-time value is only useful if you also choose the right representation.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.