- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 to convert Python date to Unix timestamp?
A UNIX timestamp is the total number of seconds that have been counted since the epoch. An epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch.
In this article, we going to see how to convert a python date to a UNIX timestamp.
Datetime to UNIX timestamp
Here we convert a python date to a UNIX timestamp by using the time.mktime() method. In this example, we initially imported the datetime module. Then we have declared and assigned a datetime.time/date (2022, 6, 3, 12, 0, 50). time.mktime() is the inverse function of the local time. The time.mktime() method of the time module accepts a time.struct_time object or a tuple of 9 elements and returns a float value which represents the time since epoch in seconds.
The syntax of the mktime() method is as follows.
Where, t is time.struct_time object or a tuple containing 9 elements which responds to time.struct_time object.
The timetuple() method of datetime.date instance returns an object of type time.struct_time. This object is converted to a UNIX timestamp by using the time.mktime() method.
Below is an example code to convert a python date to UNIX timestamp.
import datetime import time date_time = datetime.datetime(2022, 6, 3, 12, 0, 50) print("Given Date:",date_time) print("UNIX timestamp:", (time.mktime(date_time.timetuple())))
The output of the above code is as follows;
Given Date: 2022-06-03 12:00:50 UNIX timestamp: 1654257650.0
Datetime to UNIX Timestamp in utc timezone.
Here we can use the datetime module to convert a datetime to a UTC timestamp in Python. If you already have the datetime object in UTC, you can the timestamp() to get a UTC timestamp. This function returns the time since epoch for that datetime object. If you have the datetime object in the local timezone, first replace the timezone info and then fetch the time.
The following is an example code to convert a datetime to UNIX timestamp.
import datetime from datetime import timezone dt = datetime.datetime(2022, 6, 7) timestamp = dt.replace(tzinfo=timezone.utc).timestamp() print("The timestamp for the date 06-07-2022 is:",timestamp)
The timestamp for the date 06-07-2022 is: 1654560000.0
Datetime string to UNIX Timestamp
In this case, we convert a datetime which is represented in a string format is converted to a UNIX Timestamp.
import datetime given_date = "8/6/2022, 05:54:8" formated_date = datetime.datetime.strptime(given_date,"%m/%d/%Y, %H:%M:%S") Unix_timestamp = datetime.datetime.timestamp(formated_date) print("The Unix timestamp for the given input date is:") print(Unix_timestamp)
The Unix timestamp for the given input date is: 1659765248.0
Kickstart Your Career
Get certified by completing the courseGet Started