 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I convert a datetime to a UTC timestamp in Python?
We can use the datetime module to convert a datetime to a UTC timestamp in Python. If we already have the datetime object in UTC, then the timestamp() function can be directly used to get a UTC timestamp.
This function returns the time since epoch for that datetime object. If we have the datetime object in the local timezone, first replace the timezone info and then fetch the time. The following are the various methods to convert a datetime object into a UTC timestamp in Python.
- Using datetime.timestamp() with UTC-aware datetime
- Local time to UTC timestamp conversion
- Using calendar.timegm() with timetuple()
Using datetime.timestamp() with UTC-aware datetime
The datetime.timestamp() method in Python converts a datetime object to a Unix timestamp, which represents the number of seconds since the Unix epoch (January 1, 1970, 00:00:00 UTC). The method returns a floating-point number.
If the datetime object is already in UTC format (timezone aware), then we can directly call the timestamp() method.
Example
The following program demonstrates a basic conversion of a datetime object (in UTC format) into a timestamp using datetime.timestamp() method.
import datetime dt = datetime.datetime(2025, 5, 15, 12, 0, 0, tzinfo=datetime.timezone.utc) timestamp = dt.timestamp() print(timestamp)
Following is the output of the above code -
1742721600.0
Local time to UTC timestamp conversion
In this case, the datetime object was not in UTC format (local time), so we first need to convert it to UTC before getting the timestamp.
Example
In the following program, we create a local datetime object and assign it a timezone offset of 5 hours using the replace() method with datetime.timezone(datetime.timedelta(hours=-5)). Then we convert this datetime to a UNIX timestamp using the timestamp() method.
import datetime dt_local = datetime.datetime(2025, 5, 15, 8, 0, 0) dt_utc = dt_local.replace(tzinfo=datetime.timezone(datetime.timedelta(hours=-5))) timestamp = dt_utc.timestamp() print(timestamp)
Following is the output of the above code -
1747314000.0
Using calendar.timegm() with timetuple()
The calendar.timegm() function from the calendar module is used to convert a time tuple in UTC (Coordinated Universal Time) to a timestamp (seconds since epoch). This function is the inverse of time.gmtime(), which converts a timestamp to a time tuple.
The timetuple() method in Python converts a date or datetime object into a time tuple. Time tuples are a sequence of nine integers representing different components of time.
Example
In the following program, we create a datetime object for a specific date and time. Using the timetuple() method, we convert it into a time tuple. Then converted this tuple into a UTC timestamp calendar.timegm() function.
import datetime import calendar dt = datetime.datetime(2025, 7, 15, 12, 0, 0) timestamp = calendar.timegm(dt.timetuple()) print(timestamp)
Following is the output of the above code -
1752580800
