Signup/Sign In
LAST UPDATED: DECEMBER 11, 2019

How to get Current Time in Python?

    There are various situations when it is required to log certain data, operations or events in general. For this, it is essential to store the time so that it can be uniquely identified. This is when modules that fetch current date and time in Python come into play. We will look at how to get the current time of the user's location as well as different time zones.

    We will look at how the current time can be accessed using different Python modules.


    1. Using datetime module by creating a datetime object

    The datetime module contains a datetime class, and this class contains a method named now. This method returns a datetime object which contains the current date and time of that specific location. The strftime method is used to create a string representation of the datetime.


    Time for an example:

    from datetime import datetime
    
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Now the time is =", current_time)

    Output:

    Now the time is = 10:42:51



    2. Using datetime module by creating a time object

    A time object can be created using the datetime class from the datetime module, which contains the current time.


    Time for an example:

    from datetime import datetime
    
    now = datetime.now().time()  # time object is created
    print("now is =", now)
    print("The type of now is =", type(now))
    

    Output:

    now is = 10:46:01.797207
    The type of now is = <class 'datetime.time'>
    



    3. Using the time module

    The current date and time can be obtained using the time module, wherein a time object is created and its string representation is obtained using the strftime method.


    Time for an example:

    import time
    
    t_object = time.localtime()
    print("Type of object", type(t_object))
    
    current_time = time.strftime("%H:%M:%S", t_object)
    print("The current time is", current_time)
    

    Output:

    Type of object <class 'time.struct_time'>
    The current time is 10:49:17
    



    4. Using pyTZ module to find the time in a specific timezone

    The pytZ module is used to obtain the date and time in a specific timezone. The name of the timezone is passed as an argument to the now method of datetime module.


    Time for an example:

    from datetime import datetime
    import pytz
    
    tz_NewYork = pytz.timezone('America/New_York')
    datetime_NY = datetime.now(tz_NewYork)
    print("NewYork time now is:", datetime_NY.strftime("%H:%M:%S"))
    
    tz_Aus = pytz.timezone('Australia/Sydney')
    datetime_Aus = datetime.now(tz_Aus)
    print("Australia time now is:", datetime_Aus.strftime("%H:%M:%S"))
    

    Output:

    NewYork time now is: 01:24:38
    Australia time now is: 16:24:38
    



    Conclusion:

    In this post, we saw how to obtain the current time of our location, as well as of a different time zone. Do let us know which method came in handy for your use in the comment section below.

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS