Home Python Datetime Methods timestamp() Python datetime.timestamp() Method Definition timestamp() returns the number of seconds since 1970-01-01 00:00:00 UTC as a float. Microseconds appear as the fractional part.
For an aware datetime the result is exact: (moment - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds(). For a naive datetime Python assumes local time , so the number depends on the machine. fromtimestamp() is the inverse.
datetime.timestamp() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.timestamp(...). Import the class first, for example from datetime import datetime.
Syntax Call it with no arguments.
seconds = moment.timestamp() Call What it does datetime(2024, 3, 15, 10, 30, 45, tzinfo=timezone.utc).timestamp()Returns 1710498645.0. datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp()Returns 0.0. datetime(2024, 3, 15, 10, 30).timestamp()Treats the naive value as local time, so the result differs between machines.
date objects have no timestamp(); combine them with a time first.
Method signatures datetime.timestamp()
# returns: float seconds since 1970-01-01 00:00:00 UTC Parameters timestamp() takes no parameters.
Return Value Return Value Description float Seconds since the epoch; negative for moments before 1970.
Important
A naive value is interpreted in the local time zone (on some platforms very old or far-future naive values raise OverflowError or OSError). For values that are really UTC, call replace(tzinfo=timezone.utc) first. Floats hold about 15-16 significant digits, so microseconds are exact only for moments within a few centuries of 1970.
Python Examples 1. Aware values 2. Fractions and milliseconds 3. Naive UTC values Example 1 — Get the Timestamp of Aware datetimes The same instant in UTC and in UTC+05:30 gives the same number.
from datetime import datetime, timedelta, timezone
utc_moment = datetime(2024, 3, 15, 10, 30, 45, tzinfo=timezone.utc)
ist_moment = utc_moment.astimezone(timezone(timedelta(hours=5, minutes=30)))
print(utc_moment.timestamp())
print(ist_moment.timestamp())
print(datetime(1970, 1, 1, tzinfo=timezone.utc).timestamp()) 1710498645.0
1710498645.0
0.0 How It Works Float result. Even a whole number of seconds is returned as a float, so it prints with .0.Zone does not matter. 16:00:45 at UTC+05:30 is the same instant, so the timestamp is identical.Epoch is zero. Midnight UTC on 1 January 1970 is timestamp 0.0.Example 2 — Keep Microseconds and Make Milliseconds Half a second appears as .5. Multiply by 1000 and round for millisecond APIs, then convert back.
from datetime import datetime, timezone
moment = datetime(2024, 3, 15, 10, 30, 45, 500000, tzinfo=timezone.utc)
seconds = moment.timestamp()
print(seconds)
print(int(seconds), round(seconds * 1000))
print(datetime.fromtimestamp(seconds, tz=timezone.utc) == moment) 1710498645.5
1710498645 1710498645500
True How It Works Microseconds as fraction. 500000 microseconds become .5 of a second.Whole seconds and millis. int() truncates; round() after multiplying by 1000 gives an exact millisecond count.Round trip. fromtimestamp() with tz=timezone.utc rebuilds the same aware value.Example 3 — Get a Safe Timestamp from a Naive UTC Value Label a naive value as UTC before calling timestamp(), and see a moment just before the epoch.
from datetime import datetime, timezone
naive_utc = datetime(2000, 1, 1, 0, 0)
safe = naive_utc.replace(tzinfo=timezone.utc).timestamp()
print(safe)
print(naive_utc.tzinfo is None)
before = datetime(1969, 12, 31, 23, 59, tzinfo=timezone.utc)
print(before.timestamp()) How It Works Avoid local guessing. Calling timestamp() on the naive value would assume local time; attaching UTC makes the result 946684800.0 everywhere.Original untouched. replace() returns a new object, so naive_utc is still naive.Negative timestamps. One minute before the epoch is -60.0 seconds.Frequently Asked Questions How do I convert a datetime to a Unix timestamp in Python? Use moment.timestamp() on an aware datetime, for example datetime(2024, 3, 15, tzinfo=timezone.utc).timestamp().
Why does timestamp() give a different value on another computer? The datetime is naive, so Python used each machine's local time zone. Make it aware first.
How do I get a timestamp in milliseconds? Use round(moment.timestamp() * 1000).
Does date have a timestamp() method? No. Convert with datetime.combine(d, time(), tzinfo=timezone.utc).timestamp().
How do I get an int timestamp? Use int(moment.timestamp()), or calendar.timegm(moment.utctimetuple()) for aware values.
Next: datetime.timetuple() Return a time.struct_time with year, day of year, weekday, and DST flag.
timetuple() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
16 people found this page helpful
Helpful Share Copy link Suggestion