Get the Current Time in Python

Get the Current Time in Python

Vaibhav Vaibhav Jan-23, 2022 Oct-29, 2021 Python Python DateTime

Python is a versatile programming language with the generous support of libraries and modules to perform general tasks efficiently and with less code. One such domain is date and time. Dealing with dates and times can be a frustrating task at times. But Python has good support for it as well. Python has dedicated libraries that ease the pain of dealing with dates and times. They make it easier to perform operations over dates and times and perform mathematical functions such as addition and difference. They make performing conversions a piece of cake, giving developers more time to worry about the business login. This article will talk about getting the current time using one such Python module.

Get the Current Time Using the datetime Module in Python

The datetime module has a now() function that can be used to get the current date and time in Python. The following code depicts how to use this function to get the current time. Note that the now() function returns the local time instead of the UTC time. To get the UTC time, we can use the utcnow() function.

from datetime import datetime

date = datetime.now()
print(date)

Output:

2021-10-17 17:03:40.456934

As we can see, the output has both date and time. We can further call functions to get individual values. We can use the date() function to get the date and time() function to get the time. Refer to the following code for the same.

from datetime import datetime

date = datetime.now()
print(date)
print("Date:", date.date())
print("Time:", date.time())	

Output:

2021-10-17 17:09:45.754117
Date: 2021-10-17
Time: 17:09:45.754117

Furthermore, we can also use the strftime() function to format the output of the three function calls above. Following is an example of the same.

from datetime import datetime

date = datetime.now()
print("Before Formatting")
print("DateTime:", date)
print("Date:", date.date())
print("Time:", date.time())
print("After Formatting")
print("DateTime:", date.strftime("%d %B, %Y %H:%M:%S")) # DD Month, YYYY HH:MM:SS
print("Date:", date.date().strftime("%d %B, %Y")) # DD Month, YYYY
print("Time:", date.time().strftime("%H:%M:%S")) # HH:MM:SS

Output:

Before Formatting
DateTime: 2021-10-17 17:21:31.446824
Date: 2021-10-17
Time: 17:21:31.446824
After Formatting
DateTime: 17 October, 2021 17:21:31
Date: 17 October, 2021
Time: 17:21:31

To learn more about strftime(), refer to the official documentation here. To explore the format codes, refer here.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python DateTime

  • Convert Pandas Column to Datetime
  • Get the Current Time in Python
  • Get the Day of the Week in Python
  • Convert String to Datetime in Python
  • Find the Last Day of the Month Using Python
  • Determine Leap Year in Python