The Wayback Machine - https://web.archive.org/web/20240824020651/https://www.geeksforgeeks.org/python-datetime-to-integer-timestamp/
Open In App

Python datetime to integer timestamp

Last Updated : 04 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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!

Example 1: Integer timestamp of the current date and time

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 datetime
curr_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

Example 2:  Integer timestamp of specified date and time

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 datetime
dtime = 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

Example 3: UTC(Universal Time Coordinated) integer timestamp using calendar.timegm

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 datetime
import 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

Example 4: Particular timezone integer timestamp

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 datetime
import 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


Previous Article
Next Article

Similar Reads

Python | Pandas Timestamp.timestamp
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.timestamp() function returns the time expressed as the number of seconds that have passed since January 1, 1970. That z
3 min read
How to convert timestamp string to datetime object in Python?
Python has a module named Datetime to work with dates and times. We did not need to install it separately. It is pre-installed with the Python package itself. A UNIX timestamp is several seconds between a particular date and January 1, 1970, at UTC. Input: "2023-07-21 10:30:45"Output: 2023-07-21 10:30:45Explanation: In This, We are converting a tim
2 min read
How to Convert DateTime to UNIX Timestamp in Python ?
The Unix timestamp is a single signed integer that grows by one every second, allowing computers to store and manipulate conventional date systems. The software is then translated into a human-readable format. The Unix timestamp is the number of seconds calculated since January 1, 1970. In this article, we are going to see how to convert DateTime t
5 min read
Convert Datetime to UTC Timestamp in Python
Dealing with datetime objects and timestamps is a common task in programming, especially when working with time-sensitive data. When working with different time zones, it's often necessary to convert a datetime object to a UTC timestamp. In Python, there are multiple ways to achieve this. In this article, we will explore four different methods for
3 min read
Convert datetime to unix timestamp in SQLAlchemy model
When dealing with databases date and time are considered to be one of the most important attributes for any entity. With such data, we often encounter some common task of converting DateTime to a Unix timestamp. In this article, we will learn how we can convert datetime to Unix timestamp in SQLAlchemy. Converting Datetime to Unix TimeStamp in SQLal
5 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
How to convert DateTime to integer in Python
Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it separately. Here, we deal with a special date objec
2 min read
How to Convert Integer to Datetime in Pandas DataFrame?
Let's discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame. Syntax of pd.to_datetimedf['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)Create the DataFrame to Convert Integer to Datetime in Pandas Check data type for the 'Dates' column is Integer. Python
2 min read
Python | Pandas Timestamp.second
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.second attribute return an integer value which represents the value of second in the given Timestamp object. Syntax : T
2 min read
Python | Pandas Timestamp.date
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.date() function return a datetime object with same year, month and day as that of the given Timestamp object. Syntax :
2 min read
Python | Pandas Timestamp.ctime
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.ctime() function return the time in string format. The format of the returned string object is based on ctime() style.
2 min read
Get UTC timestamp in Python
Datetime module supplies classes to work with date and time. These classes provide a number of functions to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Note: For more information, refer to Python datetime module
1 min read
Python | Pandas Timestamp.is_year_start
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.is_year_start attribute return a boolean value. It return True if the date in the given Timestamp object is the start o
2 min read
Python | Pandas Timestamp.microsecond
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.microsecond attribute return the value of microsecond in the given Timestamp object. Syntax : Timestamp.microsecond Par
2 min read
Python | Pandas Timestamp.day
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.day attribute return the value of the day in the given Timestamp object. Syntax : Timestamp.day Parameters : None Retur
2 min read
Python | Pandas Timestamp.time
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.time() function return time object with same time but with tzinfo equal to None. Syntax :Timestamp.time() Parameters :
2 min read
Python | Pandas Timestamp.dayofweek
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.dayofweek attribute return the value of day of the week in the given Timestamp object. The days are numbered from 0 to
2 min read
Python | Pandas Timestamp.dayofyear
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.dayofyear attribute return the day of the year for the given Timestamp object. The days are numbered from 1 to 365. whe
2 min read
Python | Pandas Timestamp.days_in_month
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.days_in_month attribute return the number of days in the month for the given date in the Timestamp object. Syntax : Tim
2 min read
Python | Pandas Timestamp.daysinmonth
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.daysinmonth attribute return the number of days in the month for the given date in the Timestamp object. Syntax : Times
2 min read
Python | Pandas Timestamp.hour
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.hour attribute return the value of the hour for the given Timestamp object. Syntax : Timestamp.hour Parameters : None R
2 min read
Python | Pandas Timestamp.is_leap_year
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.is_leap_year attribute return a boolean value. It return True if the date in the given Timestamp object is a leap year
2 min read
Python | Pandas Timestamp.is_month_end
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.is_month_end attribute return a boolean value. It return True if the date in the given Timestamp object is the last day
2 min read
Python | Pandas Timestamp.is_month_start
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.is_month_start attribute return a boolean value. It return True if the date in the given Timestamp object is the first
2 min read
Python | Pandas Timestamp.is_quarter_end
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.is_quarter_end attribute return a boolean value. It return True if the date in the given Timestamp object is the quarte
2 min read
Python | Pandas Timestamp.minute
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.minute attribute return the minute value in the given Timestamp object. If the minute value has not been set then it re
2 min read
Python | Pandas Timestamp.month
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.month attribute is used to find the month value in the given Timestamp object. Syntax : Timestamp.month Parameters : No
2 min read
Python | Pandas Timestamp.nanosecond
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.nanosecond attribute return the nanosecond value of the given Timestamp object. Syntax : Timestamp.nanosecond Parameter
2 min read
Practice Tags :