How to Get Current Date and Time in Python

Posted in /  

How to Get Current Date and Time in Python
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    This tutorial will discuss how to use the datetime module to get the current date and time. We will also convert the three default datetime to different formats using the strftime method() .

    How to Get the Current Date and Time | Python Get Current Date

    There are two methods in datetime to print the current date and time. We can use the now() 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 the same and gave us the current date and time. The time and date we get using the now() and today() methods are very hard to read.

    Let’s use the strftime() method to format the date and time so the 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. Later, we used 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.

    How to Get the Current Date only

    When we use the today() or now() method to print the current date and time, it default gives the complete date and time at once. What if we only want the date, not the time?

    For this, we can do formatting using the strftime() method as we have done in the above example, but there is a problem. We get a string data type whenever we use the strftime() method to separate the date and time.

    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'>

    How to Get the Current Time only

    We can use the now() method in Python to get the current time in a 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'>

    Conclusion

    There are two methods to retrieve the current date and time in Python: today () and now(). To get only the current date, you can use the date.today() method. On the other hand, if you want only the current time, use the datetime.now().time() method. However, do not forget to import the datetime module into your program.

    People are also reading:

    Leave a Comment on this Post

    0 Comments