1. Python Modules for Handling Time
When it comes to working with time in Python, you'll commonly use three modules: time, datetime, and calendar.
Python represents time in three main ways:
- Timestamp: A 10-digit integer followed by decimal places, like 1758503400.123456
- Tuple (struct_time): A 9-element tuple, for example (tm_year=2025, tm_mon=9, tm_mday=22, tm_hour=10, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=265, tm_isdst=0)
- Formatted string: A human-readable time string such as '2025-09-22 10:30:00'
The time module uses tuples (struct_time) as its core for converting between timestamps and formatted strings. Meanwhile, the datetime module relies on datetime class instances for these conversions.
2. Python's time Module
The time module is implemented by calling C libraries, which means it might not work on all platforms. Most of its interfaces match the C standard library's time.h.
Let's see how to use it for converting between formatted time strings and timestamps.
2.1 Converting Timestamp to Formatted String
Get the current timestamp with the time module:
>>> import time
>>> time.time()
1760428192.7795148 
Convert a timestamp to a tuple (struct_time):
>>> time.localtime(time.time())
time.struct_time(tm_year=2025, tm_mon=10, tm_mday=14, tm_hour=15, tm_min=50, tm_sec=23, tm_wday=1, tm_yday=287, tm_isdst=0) Convert the tuple (struct_time) to a formatted string:
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
'2025-10-14 15:50:59' 2.2 Converting Formatted String to Timestamp
Let's convert the formatted string '2025-09-22 10:30:00' to a timestamp.
First, convert the formatted string to a tuple (struct_time):
>>> str_time = '2025-09-22 10:30:00'
>>> time.strptime(str_time, '%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2025, tm_mon=9, tm_mday=22, tm_hour=10, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=265, tm_isdst=-1) Then convert the tuple (struct_time) to a timestamp:
>>> time.mktime(time.strptime(str_time, '%Y-%m-%d %H:%M:%S'))
1758508200.0 2.3 Getting Current Date and Time with time Module
Get today's date using the time module (it uses the current time's struct_time by default):
>>> time.strftime('%Y-%m-%d')
'2025-09-22' Get the current time (also uses the current struct_time by default):
>>> time.strftime('%H:%M:%S')
'10:30:00' 3. Python's datetime Module
Compared to the time module, datetime offers more intuitive interfaces and greater functionality. It provides classes for handling dates and times, with options for both simple and complex operations. While it supports date and time arithmetic, its real strengths lie in formatting output and efficiently extracting properties.
3.1 Classes Defined in datetime Module
The datetime module includes these classes (all their objects are immutable):
datetime.date: Represents a date, with key attributes year, month, and daydatetime.time: Represents a time, with key attributes hour, minute, second, and microseconddatetime.datetime: Represents a specific date and timedatetime.timedelta: Represents the difference between two dates, times, or datetimes, with precision down to microsecondsdatetime.tzinfo: Abstract base class for time zone-related objects, used by time and datetime classesdatetime.timezone: Added in Python 3.2, this class implements the tzinfo abstract base and represents a fixed offset from UTC
3.2 Converting Timestamp to Formatted String with datetime Class
Convert a timestamp to a datetime instance:
>>> import time
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(time.time())
>>> dt
datetime.datetime(2025, 9, 22, 10, 30, 0, 123456) Convert the datetime instance to a formatted string:
>>> dt.strftime('%Y-%m-%d %H:%M:%S')
'2025-09-22 10:30:00' 3.3 Converting Formatted String to Timestamp with datetime Class
Convert the formatted string '2025-09-22 10:30:00' to a datetime instance:
>>> st = '2025-09-22 10:30:00'
>>> dt = datetime.strptime(st, '%Y-%m-%d %H:%M:%S')
>>> dt
datetime.datetime(2025, 9, 22, 10, 30, 0) Convert the datetime instance to a tuple (struct_time):
>>> tp = dt.timetuple()
>>> tp
time.struct_time(tm_year=2025, tm_mon=9, tm_mday=22, tm_hour=10, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=265, tm_isdst=-1) Convert the tuple (struct_time) to a timestamp:
>>> time.mktime(tp)
1758503400.0 Or you can directly use the datetime instance's timestamp() method:
>>> dt.timestamp()
1758503400.0 3.4 Getting Current Date and Time with datetime Class
Get today's date using the datetime class:
>>> datetime.now().date().strftime('%Y-%m-%d')
'2025-09-22' Get the current time using the datetime class:
>>> datetime.now().time().strftime('%H:%M:%S')
'10:30:00'