Get current time Python
www.igiftidea.com
To get the current time in Python, you can use the datetime
module from the standard library. Here's an example:
import datetime # Get the current time current_time = datetime.datetime.now().time() # Print the current time print("Current time:", current_time)
This will output the current time in the following format:
Current time: 14:45:00.123456
If you want to display the time in a specific format, you can use the strftime()
method. Here's an example:
import datetime # Get the current time current_time = datetime.datetime.now().time() # Format the time formatted_time = current_time.strftime("%H:%M:%S") # Print the formatted time print("Current time:", formatted_time)
This will output the current time in the following format:
Current time: 14:45:00
In this example, the %H
, %M
, and %S
codes represent the hour, minute, and second respectively. You can use different codes to format the time in a different way.