Python String to timestamp
In this article, you are going to look at 3 different methods to convert a string to a timestamp in Python.
Timestamp in Python is a representation of time in seconds ⏲️ since 00:00:00 UTC on 1 January 1970, as a floating point number. It is a way to represent time in a language- and system- independent manner.
The timestamp is an efficient way to store time in a database or file 🗄️. It takes less space as well as it is easier to sort and query as compared to storing the date and time in a string format.
So while working with time in Python 👨💻, you might need to convert a string to a timestamp very often.
Let's see how to do that in Python.
1. Python String to timestamp using datetime module
The datetime module in Python provides a datetime class, which is used to represent a date and time in Python.
This class provides a method called strptime() which converts a string to a DateTime object using the specified format.
Now this datetime object can be converted to timestamp using the timestamp() method.
Let's see how to do that.
# importing datetime class from datetime module
from datetime import datetime
str = "2022-01-25 12:30:00"
# Step 1: convert string to datetime object
dt = datetime.strptime(str, "%Y-%m-%d %H:%M:%S")
# Step 2: convert datetime object to timestamp
timestamp = dt.timestamp()
print("Timestamp:", timestamp)
Timestamp: 1643094000.0
Here is another example:
# importing datetime as dt
import datetime as dt
str1 = "2020/01/10"
# Step 1: convert string to datetime object
dt1 = dt.datetime.strptime(str1, "%Y/%m/%d")
# Step 2: convert datetime object to timestamp
timestamp1 = dt1.timestamp()
print("Timestamp:", timestamp1)
Timestamp: 1578594600.0
Visit string to datetime in Python to learn more about datetime module.
2. String to timestamp using time module
The time module in Python provides a method called strptime() which converts a string to a time object using the specified format.
Now this time object can be converted to timestamp using the mktime() method.
Let's see how to do that.
# importing time module
import time
str2 = "2020-01-10 12:30:00"
# Step 1: convert string to time object
t = time.strptime(str2, "%Y-%m-%d %H:%M:%S")
# Step 2: convert time object to timestamp
timestamp2 = time.mktime(t)
print("Timestamp:", timestamp2)
Timestamp: 1578626200.0
3. String to timestamp using dateutil module
To convert a string to timestamp using dateutil module, first parse the string using parser.parse() method.
Then convert the parsed string to timestamp using timestamp() method.
Here is an example:
# importing dateutil module
from dateutil import parser
string = "2023-01-29 12:30:00"
dt = parser.parse(string)
timestamp = dt.timestamp()
print("Timestamp:", timestamp)
Timestamp: 1672544600.0
Conclusion
Conversion of string to timestamp is very easy in Python. You can use any of the above methods according to your need and requirement.
Hope you liked this tutorial. Check other Python tutorials from here.
Happy Learning!😇