Python datetime to integer timestamp - GeeksforGeeks

04 Sep, 2021
In this article, we are going to see how to convert python DateTime to integer timestamp. 
The timestamp() function returns the time expressed as the number of seconds that have passed since January 1, 1970. That zero moment is known as the epoch. First, we will get the current time or explicitly mention the required date and time we want the timestamp of. There are several ways to get the date and time. We will see them as we go through the examples. Then we will convert the datetime into timestamp using timestamp() function.
At last, we will round off the timestamp in seconds and milliseconds and explicitly typecast into integer datatype and our work is done!
Here we import the DateTime module to use the DateTime function from it. And then use datetime.now() function to get the current date and time. Convert the DateTime object into timestamp using DateTime.timestamp() method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.
Python3
from datetime import datetimecurr_dt = datetime.now() print("Current datetime: ", curr_dt)timestamp = int(round(curr_dt.timestamp())) print("Integer timestamp of current datetime: ",      timestamp)
Output:
Current datetime:  2021-08-25 15:04:33.794484
Integer timestamp of current datetime:  1629884074
Give the date and time as parameters inside the datetime() function. Convert the datetime object into timestamp using datetime.timestamp() method. We will get the timestamp in seconds. Round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds. We can also convert it into milliseconds by multiplying it by1000 to get the integer timestamp in milliseconds.
Python3
from datetime import datetimedtime = datetime(2018, 1, 1, 20)print("Datetime: ", dtime) dtimestamp = dtime.timestamp()print("Integer timestamp in seconds: ",      int(round(dtimestamp))) milliseconds = int(round(dtimestamp * 1000))print("Integer timestamp in milliseconds: ",      milliseconds)
Output:
Datetime:  2018-01-01 20:00:00
Integer timestamp in seconds:  1514817000
Integer timestamp in milliseconds:  1514817000000
First, we enter the UTIC time inside the datetime.datetime() object. Then we pass the object to d.timtuple() function which gives a tuple containing the parameters like year, month, day, and so on, and then using the calendar function we convert the datetime to integer UTC timestamp.
Python3
import datetimeimport calendar d = datetime.datetime(1970, 1, 1, 2, 1, 0)ttuple = d.timetuple() itimestamp = calendar.timegm(ttuple)print("Timestamp in integer since epoch:",      itimestamp)
Output:
Timestamp in integer since epoch: 7260
First, we get the current time using datetime.datetime.now(). And then import the pytz library to instantiate the timezone object to localize the datetime. Convert the datetime object into timestamp using datetime.timestamp() method. We will get the timestamp in seconds. Round off and convert the timestamp in integer to get the integer timestamp.
Python3
import datetimeimport pytz dtime = datetime.datetime.now()timezone = pytz.timezone("Asia/Kolkata")dtzone = timezone.localize(dtime) print("Time Zone: ", dtzone.tzinfo)print("Datetime: ", dtzone) tstamp = dtzone.timestamp()print("Integer timestamp: ", int(round(tstamp)))
Output:
Time Zone:  Asia/Kolkata
Datetime:  2021-08-25 15:09:05.194413+05:30
Integer timestamp:  1629884345
saurabh1990aror
Picked
Python datetime-program
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python
*args and **kwargs in Python