To convert a string to a timestamp in Python, parse it with datetime.strptime(), attach a time zone, then call .timestamp(). You get a float of seconds since January 1, 1970 UTC:
dt = datetime.strptime("2026-03-15 09:30:00", "%Y-%m-%d %H:%M:%S")
ts = dt.replace(tzinfo=ZoneInfo("America/New_York")).timestamp() # 1773581400.0
In this tutorial, I’ll show how to convert a string to a timestamp in Python for plain dates, ISO strings and pandas columns, plus the format error you’ll likely hit. Outputs are from Python 3.12.5 and pandas 3.0.6.
Convert a string to a Unix timestamp with strptime() and timestamp()
strptime() turns the text into a datetime using format codes like %Y and %H; my guide to converting a string to datetime in Python lists them. Here’s an order placed in New York:
from datetime import datetime
from zoneinfo import ZoneInfo
order_time = "2026-03-15 09:30:00"
dt = datetime.strptime(order_time, "%Y-%m-%d %H:%M:%S")
dt = dt.replace(tzinfo=ZoneInfo("America/New_York"))
ts = dt.timestamp()
print(dt)
print(ts) # seconds since 1970-01-01 UTC
print(int(ts)) # whole seconds
print(int(ts * 1000)) # milliseconds for JavaScript
Output:
2026-03-15 09:30:00-04:00
1773581400.0
1773581400
1773581400000

Why does the same date string give different timestamps?
A Unix timestamp is one moment in UTC, so a wall-clock string isn’t enough on its own. Same text, four zones:
import time
from datetime import datetime
from zoneinfo import ZoneInfo
s, fmt = "2026-03-15 09:30:00", "%Y-%m-%d %H:%M:%S"
for zone in ["America/New_York", "America/Chicago", "America/Los_Angeles", "UTC"]:
dt = datetime.strptime(s, fmt).replace(tzinfo=ZoneInfo(zone))
print(f"{zone:<20} {dt.timestamp():.0f}")
naive = datetime.strptime(s, fmt)
print("naive = local time:", naive.timestamp() == time.mktime(naive.timetuple()))
Output:
America/New_York 1773581400
America/Chicago 1773585000
America/Los_Angeles 1773592200
UTC 1773567000
naive = local time: True

Without tzinfo, .timestamp() quietly uses your computer’s local zone, exactly like time.mktime(). The difference is explained in naive vs aware datetime in Python.
Convert an ISO 8601 string to a timestamp
When the string already carries an offset, skip the format codes. fromisoformat() reads it, and you can use a trailing Z on Python 3.11+:
from datetime import datetime
for s in ["2026-03-15T09:30:00-04:00", "2026-03-15T13:30:00Z"]:
print(f"{s:<26} {datetime.fromisoformat(s).timestamp():.0f}")
Output:
2026-03-15T09:30:00-04:00 1773581400
2026-03-15T13:30:00Z 1773581400
Both print 1773581400, because they’re the same moment. If you start from a datetime object instead of text, see converting datetime to a Unix timestamp in Python.
Convert a pandas string column to Unix timestamps
For a column, use pd.to_datetime() with a format, localize it, then count seconds from the epoch:
import pandas as pd
orders = pd.DataFrame({
"order_id": [1041, 1042, 1043],
"placed": ["2026-03-15 09:30:00", "2026-03-15 14:05:30", "2026-03-16 08:00:00"],
})
placed = pd.to_datetime(orders["placed"], format="%Y-%m-%d %H:%M:%S")
placed = placed.dt.tz_localize("America/Chicago")
print(placed.dtype)
print("astype int64:", placed.astype("int64").iloc[0]) # microseconds here!
epoch = pd.Timestamp("1970-01-01", tz="UTC")
orders["unix"] = (placed - epoch) // pd.Timedelta(seconds=1)
print(orders)
Output:
datetime64[us, America/Chicago]
astype int64: 1773585000000000
order_id placed unix
0 1041 2026-03-15 09:30:00 1773585000
1 1042 2026-03-15 14:05:30 1773601530
2 1043 2026-03-16 08:00:00 1773666000

astype("int64") is a million times bigger than the seconds column.Older answers divide astype("int64") by 10**9, assuming nanoseconds. On pandas 3 that’s wrong, so I subtract the epoch. More parsing options are in converting strings to datetime in pandas.
Fix strptime() ValueError: time data does not match format
strptime() is strict: every character must match the format. Both strings below fail:
from datetime import datetime
fmt = "%Y-%m-%d %H:%M:%S"
for s in ["03/15/2026 9:30 AM", "2026-03-15 09:30"]:
try:
datetime.strptime(s, fmt)
except ValueError as err:
print("ValueError:", err)
fixed = datetime.strptime("03/15/2026 9:30 AM", "%m/%d/%Y %I:%M %p")
print("fixed:", fixed)
Output:
ValueError: time data '03/15/2026 9:30 AM' does not match format '%Y-%m-%d %H:%M:%S'
ValueError: time data '2026-03-15 09:30' does not match format '%Y-%m-%d %H:%M:%S'
fixed: 2026-03-15 09:30:00

%m/%d/%Y %I:%M %p parses the first one.Match the format to the data, using %I and %p for 12-hour times. To test strings before converting, see checking if a string is a valid date in Python.
How do I convert a timestamp back to a string?
Pass the time zone to fromtimestamp(), then format with strftime():
from datetime import datetime
from zoneinfo import ZoneInfo
ts = 1773581400
dt = datetime.fromtimestamp(ts, tz=ZoneInfo("America/New_York"))
print(dt.strftime("%B %d, %Y %I:%M %p %Z"))
Output:
March 15, 2026 09:30 AM EDT
That’s the original order time again. For more patterns, see converting epoch time to datetime in Python.
Working with dates and times? Try these next:
- Convert a string to a date in Python
- Get the current date and time in Python
- Convert local time to UTC in Python
- Convert datetime to a string in Python
Frequently asked questions
How do I convert a string to a timestamp in Python?
Parse it with datetime.strptime(), set tzinfo, and call .timestamp().
How do I get a timestamp in milliseconds?
Use int(dt.timestamp() * 1000).
Why is my timestamp off by a few hours?
The datetime was naive, so Python used your local time zone. Attach a ZoneInfo first.
Can I convert an ISO string with a Z at the end?
Yes. datetime.fromisoformat() accepts Z on Python 3.11 and newer.
How do I convert a pandas column to Unix seconds?
Subtract pd.Timestamp("1970-01-01", tz="UTC") and floor-divide by pd.Timedelta(seconds=1).
Where is datetime.timestamp() documented?
See datetime.timestamp() in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn