How to Convert Timestamps in Python?

Estimated read time 3 min read

To convert timestamps in Python, you can use the datetime module, which provides various functions for working with dates and times. There are multiple ways to convert timestamps depending on the format of the timestamp. Here are a few common scenarios:

  1. Unix Timestamp to Datetime: If you have a Unix timestamp (representing the number of seconds since January 1, 1970), you can use the datetime.fromtimestamp() function to convert it to a datetime object.
from datetime import datetime

timestamp = 1622207400  # Example Unix timestamp

datetime_obj = datetime.fromtimestamp(timestamp)
print(datetime_obj)

In this example, we use the datetime.fromtimestamp() function and pass the Unix timestamp as an argument. The function converts the timestamp to a datetime object, which we assign to the variable datetime_obj. Finally, we print the datetime_obj, which displays the converted datetime.

  1. String Timestamp to Datetime: If you have a timestamp in string format, you can use the datetime.strptime() function to parse the string and convert it to a datetime object. You need to specify the format of the string timestamp using the appropriate format codes.
from datetime import datetime

timestamp_string = "2021-05-28 12:34:56"  # Example string timestamp
format_string = "%Y-%m-%d %H:%M:%S"  # Format of the string timestamp

datetime_obj = datetime.strptime(timestamp_string, format_string)
print(datetime_obj)

In this example, we use the datetime.strptime() function and pass the timestamp string and its format as arguments. The function parses the string according to the specified format and returns a datetime object, which we assign to the variable datetime_obj. Finally, we print the datetime_obj.

  1. Other Timestamp Formats: If you have timestamps in other formats, you can use the datetime module’s functions to parse and convert them. For example, you can use datetime.strptime() with the appropriate format string, or you can split the timestamp string into its components and create a datetime object using the datetime() constructor.
from datetime import datetime

# Example: Parsing a timestamp with a custom format
timestamp_string = "28-05-2021 12:34:56"
format_string = "%d-%m-%Y %H:%M:%S"
datetime_obj = datetime.strptime(timestamp_string, format_string)
print(datetime_obj)

# Example: Creating a datetime object from timestamp components
year = 2021
month = 5
day = 28
hour = 12
minute = 34
second = 56
datetime_obj = datetime(year, month, day, hour, minute, second)
print(datetime_obj)

In these examples, we demonstrate parsing a timestamp with a custom format using datetime.strptime(), and creating a datetime object by specifying the individual components (year, month, day, hour, minute, second) to the datetime() constructor.

Choose the appropriate approach based on the format of the timestamp you’re working with. The datetime module provides a range of functions to handle different timestamp formats and convert them to datetime objects for further manipulation and analysis.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply