Get Current Timestamp In Python

Saturday, Aug 10, 2024 | 3 minutes read | Update at Saturday, Aug 10, 2024

@ İsmail Baydan

Python provides different modules and methods in order to get current timestamp. In this tutorial we examine these modules and functions in detail with examples. Here are 10 examples of how to get the current timestamp in Python using different methods:

  1. Using time module: The builtin time module provides the time() method in order to get current timestamp. Do not confuse the time module with the time() method as you can see below.

    import time
    timestamp = time.time()
    print(timestamp)
    

    This returns the current time as a float representing seconds since the epoch.

  2. Using datetime module: Another time and date related module to get current timestamp is the datetime module. The datetime module provides the now() method which returns current date and time. Then we can use the timestamp() method to get current timestamp.

    from datetime import datetime
    timestamp = datetime.now().timestamp()
    print(timestamp)
    

    This returns the current time as a float representing seconds since the epoch.

  3. Using datetime module to get the current datetime:

    from datetime import datetime
    current_datetime = datetime.now()
    print(current_datetime)
    

    This returns the current date and time as a datetime object.

  4. Using datetime with strftime for a formatted timestamp: We can format the timestamp like a string. The strftime() method is used to return current timestamp as formatted string.

    from datetime import datetime
    formatted_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(formatted_timestamp)
    

    This returns the current date and time as a formatted string.

  5. Using time module for a formatted timestamp: We can use the time module strftime() and the time.localtime() methods to get current timestamp .

    import time
    formatted_timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print(formatted_timestamp)
    

    This returns the current date and time as a formatted string using the time module.

  6. Using pandas to get the current timestamp: Python provides different 3rd party libraries to get current timestamp. The pandas module provides the Timestamp.now() to return current timestamp.

    import pandas as pd
    timestamp = pd.Timestamp.now()
    print(timestamp)
    

    This returns the current timestamp as a pandas.Timestamp object.

  7. Using datetime module with timezone information: We can get current timestamp for different timezones. We can use the datetime.now() method by providing the timezone information to get specified timezone timestamp.

    from datetime import datetime, timezone
    timestamp_with_timezone = datetime.now(timezone.utc)
    print(timestamp_with_timezone)
    

    This returns the current date and time with UTC timezone information.

  8. Using datetime to get the current date only: We can get timestamp only for date by calling datetime module now() method and then calling date() method for only date part not time part.

    from datetime import datetime
    current_date = datetime.now().date()
    print(current_date)
    

    This returns the current date only, without the time.

  9. Using datetime to get the current time only: We can get timestamp only for date by calling datetime module now() method and then calling time() method for only date part not time part.

    from datetime import datetime
    current_time = datetime.now().time()
    print(current_time)
    

    This returns the current time only, without the date.

  10. Using arrow library (third-party) to get the current timestamp: The 3rd party arrow module provides now() method in order to get current timestamp.

    import arrow
    timestamp = arrow.now()
    print(timestamp)
    

    This returns the current timestamp as an arrow object, which provides more flexibility with date and time manipulation.

These examples cover different ways to work with timestamps, dates, and times in Python.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.