How to get current date and time in Python

Posted in /  

How to get current date and time in Python

Vinay Khatri
Last updated on June 9, 2022

    In this tutorial, we will discuss how can we use the datetime module to get the current date and time. We will also convert the 3 default datetime to different formats using strftime method().

    Get the current date and time

    There are two methods in datetime which can print the current date and time. Whether we can use the now() method or today() method to get the current time and date. Example:

    from datetime import datetime
    
    now = datetime.now()
    today = datetime.today()
    
    print('Get date and time using now() method=',now)
    print('Get date and time using today() method=',today)

    Output:

    Get date and time using now() method= 2019-08-06 20:54:01.297468
    Get date and time using today() method= 2019-08-06 20:54:01.297468

    Behind the code: In the above example, we have used two methods now() and today() both worked same and give us the current date and time. The time and date we get using the now() and today() method is very hard to read, let’s use the strftime() method to format the date and time sothe user can easily read it. Example:

    from datetime import datetime
    
    now = datetime.now()
    
    date = now.strftime('%d %B, %Y')
    time = now.strftime('%H:%M')
    
    print("Today's date is=",date)
    print('Time is =',time)

    Output:

    Today's date is= 06 August, 2019
    Time is = 21:47

    Behind the code: In the above example, we used the now() method to get the current date and time, then we use the strftime() method to format the datetime object now. The date and time variables are string by nature. We have used the format codes inside the strftime() method.

    Get the current date only:

    When we use the today() or now() method to print the current date and time, by default it gives the complete date and time at once, what if we only want date, not time? For this, we can do formatting using the strftime() method as we have done in the above example, but there is a problem with that, whenever we the strftime() method to separate date and time we get a string data type. There is another approach to get the current date and the date type of the date would remain datetime, but the date we get it would be in the default format. To get the current date only we use the date.today() method: Example:

    from datetime import date
    
    today_date = date.today()
    
    print("Today is: ", today_date)
    print("The type of today_date is: ", type(today_date))

    Output

    Today is:  2019-08-06
    The type of today_date is:  <class 'datetime.date'>

    Get the current time only:

    In python, we can use the now() method to get the current time in datetime object, without using the strftime() method. Example:

    from datetime import datetime
    
    current_time = datetime.now().time()
    
    print("Time is : ", current_time)
    print("The type of current_time is: ", type(current_time))

    Output:

    Time is :  21:40:13.044270
    
    The type of current_time is:  <class 'datetime.time'>

    Python Tutorials


    Leave a Comment on this Post

    0 Comments