Python Unix Timestamp Guide

    Python offers multiple ways to work with Unix timestamps through its built-in time and datetime modules. The time.time() function returns a float representing seconds since the epoch, while the datetime module provides more sophisticated date manipulation. Python 3.3+ added the timestamp() method to datetime objects for easy conversion.

    Unix timestamps are a universal way to represent a specific moment in time as a single integer, counting the seconds elapsed since January 1, 1970 (the Unix epoch). Working with timestamps in Python is a common task for developers building applications that log events, schedule jobs, compare dates, or communicate with APIs. The examples below cover the three most essential operations: retrieving the current timestamp, converting a timestamp into a human-readable date, and converting a date back into a timestamp.

    Get Current Timestamp

    The most common starting point is to capture the current moment as a Unix timestamp. In Python, you can obtain the number of seconds (or milliseconds) since the epoch using built-in functions. This value is useful for recording when an event occurred, setting cache expiry times, or generating time-based identifiers.

    Get Current Unix Timestamp

    import time
    from datetime import datetime
    
    # Get current Unix timestamp
    timestamp = int(time.time())
    print(timestamp)  # e.g., 1706745600
    
    # Using datetime
    timestamp = int(datetime.now().timestamp())

    Convert Timestamp to Date

    Once you have a Unix timestamp, you often need to display it in a human-readable format. Converting a raw integer like 1706745600 into a formatted date string such as "2024-02-01 00:00:00" makes it meaningful to end users. The following Python code demonstrates how to perform this conversion with proper timezone handling.

    Convert to Date

    from datetime import datetime, timezone
    
    # Convert Unix timestamp to datetime
    timestamp = 1706745600
    dt = datetime.fromtimestamp(timestamp)
    print(dt)  # 2024-02-01 00:00:00
    
    # UTC datetime (recommended)
    dt_utc = datetime.fromtimestamp(timestamp, tz=timezone.utc)
    print(dt_utc.isoformat())  # 2024-02-01T00:00:00+00:00

    Convert Date to Timestamp

    The reverse operation is equally important. When users provide a date through a form or when your application reads dates from a file, you need to convert them into Unix timestamps for storage, comparison, or transmission via APIs. Here is how to convert a date or date string into a Unix timestamp in Python.

    Convert to Timestamp

    from datetime import datetime
    
    # Convert datetime to Unix timestamp
    dt = datetime(2024, 2, 1, 0, 0, 0)
    timestamp = int(dt.timestamp())
    print(timestamp)  # 1706745600
    
    # From string
    dt = datetime.strptime('2024-02-01', '%Y-%m-%d')
    timestamp = int(dt.timestamp())

    Common Pitfalls in Python

    Working with timestamps can be error-prone, especially across different platforms and timezone configurations. Being aware of these common mistakes will help you write more robust Python code and avoid subtle bugs that are difficult to trace in production.

    • 1datetime.utcfromtimestamp() is deprecated in Python 3.12+ — use datetime.fromtimestamp(ts, tz=timezone.utc) instead
    • 2time.time() returns a float, not an integer — wrap with int() for standard Unix timestamps
    • 3datetime.timestamp() uses local timezone by default, which may differ from UTC
    • 4Be careful with naive vs aware datetime objects when converting to timestamps

    Best Practices for Timestamp Handling

    Regardless of the programming language, following a few best practices will keep your timestamp code reliable. Always store and transmit timestamps in UTC to avoid timezone ambiguity. Use seconds-based Unix timestamps unless your application requires sub-second precision, in which case milliseconds or microseconds are appropriate. When displaying dates to users, convert from UTC to their local timezone only at the presentation layer. Document whether your APIs expect seconds or milliseconds, as mixing the two is one of the most frequent sources of timestamp bugs.

    Frequently Asked Questions

    Related Tools

    Timestamp Guides for Other Languages

    Need Unix timestamp examples for a different programming language? Browse our complete collection of language-specific guides with copy-paste code snippets.

    We use cookies to analyze traffic and improve your experience. Learn more