Python provides excellent support for Unix timestamps through the built-in time module and the more powerful datetime module. For data analysis tasks,pandas also offers robust timestamp handling. This guide covers all essential timestamp operations in Python.
How to get the current epoch time
import time; time.time()Source
Convert from human-readable date to epoch
import calendar, time; calendar.timegm(time.strptime('2000-01-01 12:34:00', '%Y-%m-%d %H:%M:%S'))Convert from epoch to human-readable date
import time; time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(epoch))Replace time.localtime with time.gmtime for GMT time. Or using datetime: import datetime; datetime.datetime.utcfromtimestamp(epoch).replace(tzinfo=datetime.timezone.utc)
Additional Python Examples
Using datetime Module (Recommended)
import datetime
# Get current timestamp
now = datetime.datetime.now()
timestamp = now.timestamp()
print(int(timestamp)) # Unix timestamp in seconds
# Convert timestamp to datetime
dt = datetime.datetime.fromtimestamp(1609459200)
print(dt.strftime('%Y-%m-%d %H:%M:%S'))
# Convert datetime to timestamp
dt = datetime.datetime(2024, 1, 1, 12, 0, 0)
timestamp = dt.timestamp()
print(int(timestamp))The datetime module provides a more intuitive and feature-rich interface for date/time operations.
Using pandas for Timestamp Operations
import pandas as pd
# Convert timestamp to pandas Timestamp
ts = pd.Timestamp(1609459200, unit='s')
print(ts) # 2021-01-01 00:00:00
# Convert pandas Timestamp to Unix timestamp
ts = pd.Timestamp('2024-01-01 12:00:00')
unix_ts = int(ts.timestamp())
print(unix_ts)
# Working with timezone-aware timestamps
ts_utc = pd.Timestamp('2024-01-01 12:00:00', tz='UTC')
unix_ts = int(ts_utc.timestamp())
print(unix_ts)pandas provides powerful timestamp handling, especially useful for data analysis and time series operations.
UTC vs Local Time
import time
import datetime
# UTC timestamp
utc_timestamp = time.time()
utc_dt = datetime.datetime.utcfromtimestamp(utc_timestamp)
# Local timestamp
local_dt = datetime.datetime.fromtimestamp(utc_timestamp)
# Timezone-aware datetime
dt_aware = datetime.datetime.fromtimestamp(utc_timestamp, tz=datetime.timezone.utc)
print(dt_aware)Always be aware of timezone differences. Use UTC for consistency in applications.
About Python Unix Timestamps
Python provides excellent support for Unix timestamps through the built-in time and datetime modules. Whether you're working with simple timestamp operations or complex time series data using pandas, Python offers powerful and flexible tools for handling epoch time. This guide covers all essential operations including getting current timestamps, converting between timestamps and dates, and working with timezones.
Related guides: Check out our guides for other programming languages: JavaScript, Java, PHP, and more. For timestamp conversion tools, visit our Tools page.
Frequently Asked Questions
How do I get the current Unix timestamp in Python?
Use time.time() to get the current timestamp in seconds, or datetime.datetime.now().timestamp() for more precise control. The time module provides the simplest approach for Unix timestamps.
How do I convert a Unix timestamp to a date in Python?
Use datetime.datetime.fromtimestamp(timestamp) to convert a Unix timestamp to a datetime object. For UTC timestamps, use datetime.datetime.utcfromtimestamp(timestamp).
What is the difference between time.time() and datetime.now().timestamp()?
time.time() returns a float representing seconds since the epoch, while datetime.now().timestamp() returns the timestamp of the current datetime object. Both are equivalent, but datetime provides more formatting options.