Python datetime.timestamp() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
datetime 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.

python
seconds = moment.timestamp()
CallWhat 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
python
datetime.timestamp()
# returns: float seconds since 1970-01-01 00:00:00 UTC

Parameters

timestamp() takes no parameters.

Return Value

Return ValueDescription
floatSeconds since the epoch; negative for moments before 1970.

Python Examples

Example 1 — Get the Timestamp of Aware datetimes

The same instant in UTC and in UTC+05:30 gives the same number.

python
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())

How It Works

  1. Float result. Even a whole number of seconds is returned as a float, so it prints with .0.
  2. Zone does not matter. 16:00:45 at UTC+05:30 is the same instant, so the timestamp is identical.
  3. 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.

python
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)

How It Works

  1. Microseconds as fraction. 500000 microseconds become .5 of a second.
  2. Whole seconds and millis. int() truncates; round() after multiplying by 1000 gives an exact millisecond count.
  3. 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.

python
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

  1. Avoid local guessing. Calling timestamp() on the naive value would assume local time; attaching UTC makes the result 946684800.0 everywhere.
  2. Original untouched. replace() returns a new object, so naive_utc is still naive.
  3. Negative timestamps. One minute before the epoch is -60.0 seconds.

Frequently Asked Questions

Use moment.timestamp() on an aware datetime, for example datetime(2024, 3, 15, tzinfo=timezone.utc).timestamp().
The datetime is naive, so Python used each machine's local time zone. Make it aware first.
Use round(moment.timestamp() * 1000).
No. Convert with datetime.combine(d, time(), tzinfo=timezone.utc).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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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