Current Date
Time Management
Computer Tips
Tech How-To
Programming Basics

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.

Browse interview questions

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.

python
1from datetime import datetime, timezone
2
3local_now = datetime.now()
4utc_now = datetime.now(timezone.utc)
5
6print(local_now)
7print(utc_now)

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.

javascript
1const now = new Date();
2console.log(now);
3console.log(now.toISOString());
4console.log(now.toLocaleString());

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.

java
1import java.time.Instant;
2import java.time.LocalDateTime;
3import java.time.ZoneId;
4import java.time.ZonedDateTime;
5
6public class Main {
7    public static void main(String[] args) {
8        LocalDateTime localNow = LocalDateTime.now();
9        ZonedDateTime zonedNow = ZonedDateTime.now(ZoneId.systemDefault());
10        Instant utcNow = Instant.now();
11
12        System.out.println(localNow);
13        System.out.println(zonedNow);
14        System.out.println(utcNow);
15    }
16}

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:

python
1from datetime import datetime
2
3now = datetime.now()
4print(now.strftime("%Y-%m-%d %H:%M:%S"))

JavaScript:

javascript
const now = new Date();
console.log(now.toLocaleString("en-CA"));

Java:

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3
4LocalDateTime now = LocalDateTime.now();
5String text = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
6System.out.println(text);

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.

javascript
console.log(new Date().toISOString());

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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions