How to Convert a String to a Timestamp in Python?

As a data scientist working with time-series data for one of my clients in Chicago, I recently faced a challenge where I needed to convert date strings to timestamps. After experimenting with different methods, I discovered the most effective ways to convert a string to a Timestamp. I will explain how to convert a string to a timestamp in Python in this tutorial with suitable examples.

Convert a String to a Timestamp in Python

When dealing with date and time data, it’s common to encounter dates stored as strings. However, to perform time-series analysis or calculations, we often need to convert these string representations to timestamps. Python provides built-in libraries and functions to handle this conversion seamlessly.

For example, let’s say you have a dataset containing user registration dates for your US-based application. The dates are stored as strings in the format “YYYY-MM-DD HH:MM: SS”. To analyze user behavior over time, you need to convert these string dates to timestamps.

Read How to Print Strings and Integers Together in Python?

Use strptime() in Python

Python’s module datetime offers a convenient method strptime() that allows you to convert a string to a datetime object according to the DataCamp tutorial. Here’s how you can use it:

from datetime import datetime

date_string = "2023-05-15 09:30:00"
datetime_object = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
timestamp = datetime_object.timestamp()
print(timestamp)

Output:

1684123200.0

I have executed the above example code and added the screenshot below.

Convert a String to a Timestamp in Python

In this example, we have a date string "2023-05-15 09:30:00" representing May 15, 2023, at 9:30 AM. We use the strptime() method to parse the string and specify the format using directives like %Y for the year, %m for the month, %d for the day, %H for the hour, %M for the minutes, and %S for the seconds.

Read How to Truncate a String to a Specific Length in Python?

Handle Different Date Formats

In real-world scenarios, date strings can come in various formats. Python’s strptime() method allows you to handle different formats by specifying the appropriate directives. Here are a few common examples:

Example 1: MM/DD/YYYY Format

from datetime import datetime

date_string = "05/15/2023"
datetime_object = datetime.strptime(date_string, "%m/%d/%Y")
timestamp = datetime_object.timestamp()
print(timestamp)

Output:

1684089000.0

I have executed the above example code and added the screenshot below.

How to Convert a String to a Timestamp in Python

In this example, the date string follows the format “MM/DD/YYYY”. We use the directives %m for the month, %d for the day, and %Y for the year to parse the string correctly.

Read How to Get the Last 4 Characters of a String in Python?

Example 2: YYYY-MM-DD Format

from datetime import datetime

date_string = "2023-05-15"
datetime_object = datetime.strptime(date_string, "%Y-%m-%d")
timestamp = datetime_object.timestamp()
print(timestamp)

Output:

1684089000.0

I have executed the above example code and added the screenshot below.

Convert a String to a Timestamp in Python example

Here, the date string is in the format “YYYY-MM-DD”. We adjust the directives accordingly, using %Y for the year, %m for the month, and %d for the day.

Read How to Get the Last 3 Characters of a String in Python?

Convert Timestamp to Date String

Sometimes, you may need to convert a timestamp back to a human-readable date string. Python’s datetime module provides the fromtimestamp() method for this purpose.

from datetime import datetime

timestamp = 1684144200.0
datetime_object = datetime.fromtimestamp(timestamp)
date_string = datetime_object.strftime("%Y-%m-%d %H:%M:%S")
print(date_string)

In this example, we have a timestamp 1684144200.0. We use the fromtimestamp() method to convert it back to a datetime object. Then, we use the strftime() method to format the datetime object into a desired string representation. The directives used strftime() are similar to those used in strptime().

Read How to Check if a Word is in a String in Python?

Conclusion

In this tutorial, I have explained how to convert a string to a timestamp in Python. I explained strptime() method, we can easily parse date strings in various formats and convert them to timestamps. Additionally, you can use the fromtimestamp() method to convert timestamps back to human-readable date strings.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.