Whether you’re logging events, measuring performance, or scheduling tasks, you’ll often need to work with “the current time” in Python. Two of the most common ways are:
time.time()
from the built-intime
moduledatetime.now()
from thedatetime
module
In this simple guide, we’ll look at what each does, how they differ, and when to use one or the other.
1. time.time()
: A Raw Timestamp in Seconds
import time
# Get the current time as a floating-point number
ts = time.time()
print(ts)
# e.g. → 1746147839.123456
- What you get
A singlefloat
representing the number of seconds (including fractions) since the Unix epoch: 1970-01-01 00:00:00 UTC. - Key traits
- Timezone-agnostic: always measured from UTC.
- Simple to store: easy to serialize into JSON, databases, or log files.
- High precision: microsecond (or better) resolution in most systems.
When to pick time.time()
- You need a numeric timestamp for logs, analytics, or inter-language data exchange.
- You want the cheapest, fastest call with minimal overhead.
- You’ll never need to know the human-readable date or timezone—just the raw interval.
2. datetime.now()
: A Date-and-Time Object
from datetime import datetime
# Naive local datetime (no tzinfo)
now_local = datetime.now()
print(now_local)
# e.g. → 2025-05-02 14:30:22.456789
- What you get
A naivedatetime
object whose fields (year, month, day, hour, …
) reflect your system’s local clock—but with no attached timezone info (tzinfo is None
). - Key traits
- Human-readable: already broken into components.
- Rich API: built-in methods for formatting, parsing, arithmetic, and comparisons.
- Can be made timezone-aware: by passing a
tz
argument or using.astimezone()
.
Making it timezone-aware
from datetime import datetime, timezone
# UTC-aware
now_utc = datetime.now(timezone.utc)
print(now_utc)
# e.g. → 2025-05-02 06:30:22.456789+00:00
# Convert naive local into an aware datetime in your OS locale
aware_local = datetime.now().astimezone()
print(aware_local)
# e.g. → 2025-05-02 16:30:22.456789+10:00
3. Simple Use Cases
A. Logging an Event
- With
time.time()
: store a number pythonCopyEditevents = [] events.append({ "event": "user_login", "ts": time.time() })
- With
datetime.now()
: store a datetime pythonCopyEditfrom datetime import datetime events.append({ "event": "user_login", "time": datetime.now() })
If you only need to sort or calculate differences, the float from time.time()
may be simpler. If you want to display “2025-05-02 14:30” in logs, datetime
saves you extra formatting.
B. Scheduling a Reminder
- Calculate a future timestamp with
time.time()
plus seconds: pythonCopyEditremind_at = time.time() + 3600 # one hour later
- Using
datetime
arithmetic: pythonCopyEditfrom datetime import datetime, timedelta remind_dt = datetime.now() + timedelta(hours=1)
Here, timedelta
makes your code more readable and less error-prone.
4. Key Takeaways
- Use
time.time()
when you want a quick, portable, numeric UTC timestamp. - Use
datetime.now()
(especially timezone-aware) when you need human-readable dates, formatting, or date arithmetic. - You can always convert between them:
datetime → float ts = datetime.now(timezone.utc).timestamp() # float → datetime dt = datetime.fromtimestamp(ts, tz=timezone.utc)
With these simple patterns, you can pick the right tool for your logging, scheduling, and time-tracking needs.
Categories: