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 November 6, 2022

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

    How to Get the Current date and time

    There are two methods in datetime which can print the current date and time. Either we can use the now() method or the 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 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 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; then 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.

    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 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 with that, whenever we the strftime() method to separate the 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 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, namely 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 in your porgram.

    People are also reading:

    Leave a Comment on this Post

    0 Comments