Python provides various libraries to work with timestamp data. For example, the datetime and time module helps in handling the multiple dates and time formats. In addition to this, it supports various functionalities involving the timestamp and timezone. Show
After reading this tutorial, you’ll learn: –
Table of contentsWhat is Timestamp in PythonA timestamp is encoded information generally used in UNIX, which indicates the date and time at which a particular event has occurred. This information could be accurate to the microseconds. It is a POSIX timestamp corresponding to the datetime instance.
The reason we are using the UNIX epoch time as 1 January 1970 is because of the fact that UNIX came into business around that time frame. The below image shows how a particular date and time is represented in different formats. timestamp representationGet Current TimestampTo get the current timestamp in Python, use any of the following three modules.
Datetime to TimestampThe Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572465 method of a datetime module returns the POSIX timestamp corresponding to the datetime instance. The return value is float.
Example
Output: Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.357246 Note: Note: It returns timestamp in float type to get timestamp without decimal value convert it to an integer using the Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572468 constructor. Get Timestamp Using Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572469 ModuleThe time module‘s 0 method returns the current time in the timestamp format, which is nothing but the time elapsed from the epoch time, January 1, 1970.
Definition: This function returns the time in seconds since the epoch as a floating-point number.
Get Timestamp Using import time # current timestamp x = time.time() print("Timestamp:", x) # Output 1625309785.4823471 ModuleUse the calendar module’s 2 method to convert the current time to the timestamp.
Example:
Convert Timestamp to Datetime (format)While the timestamp’s default format is just a floating-point number, there are cases when the timestamp will be represented in the ISO 8601 format. This looks something like the below value with the T and Z alphabets. 2014-09-12T19:34:29Z Here the alphabet T stands for Time and Z stands for the Zero timezone which represents the offset from the coordinated universal time(UTC). Let us see few examples with different date-time formats. Based on the format we will be using the format string and we can extract the timestamp information from that. We can convert the timestamp back to 5 object using the 6 method that is available in the datetime module.Syntax
It returns the local date and time corresponding to the POSIX timestamp, such as is returned by 7.If optional argument 8 is 9 or not specified, the timestamp is converted to the platform’s local date and time, and the returned 5 object is naive.Example:
Convert Timestamp to StringWe can convert the timestamp string using the datetime formatting.
It returns the local date and time corresponding to the POSIX timestamp, such as is returned by 7.If optional argument 8 is 9 or not specified, the timestamp is converted to the platform’s local date and time, and the returned 5 object is naive.Example:
Output: Result 1: 03-07-2021, 16:21:12 Result 2: 03 July, 2021 Result 3: 04PM 21:12 Get Timestamp in MillisecondsThe datetime object comes with the timestamp which in turn could be displayed in milliseconds. Example:
Get The UTC timestampAs we discussed, we can get the timestamp from the datetime object with the timezone information. We can convert a datetime object into a timestamp using the Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572465 method. If the datetime object is UTC aware, then this method will create a UTC timestamp. If the object is naive, we can assign the UTC value to the 6 parameter of the datetime object and then call the Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572465 method. Example: Get timestamp from 5 with UTC timezoneDate and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572460 Timestamp from import time # current timestamp x = time.time() print("Timestamp:", x) # Output 1625309785.4823475 with a Different TimezoneWe have seen how to get the 2014-09-12T19:34:29Z0 information from a 5 object with a timezone set as 2014-09-12T19:34:29Z2 Similarly 2014-09-12T19:34:29Z3 we can get 2014-09-12T19:34:29Z0 information from a datetime object with a timezone different than the UTC. This could be done with 2014-09-12T19:34:29Z5 with the offset information. Read: Working with timezone in Python. Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572461 Output Date and time is: 2021-07-03 16:21:12.357246 Timestamp is: 1625309472.3572462 Convert an Integer Timestamp to DatetimeWe have seen how we can display the timestamp in milliseconds. Similarly, we can convert a timestamp value in integer to 5 object using the same 6 or 2014-09-12T19:34:29Z8() method. In the below example we are considering the timestamp in milliseconds and finding its corresponding datetime object. We are using the constant 2014-09-12T19:34:29Z9 for normalizing the value. |