In Python, there are multiple methods to retrieve the current time. One convenient option is to utilize the datetime module.
# Import the datetime module import datetime # Get the current datetime object now = datetime.datetime.now() # Print the datetime object print(now) # Output: datetime.datetime(2023, 3, 8, 14, 35, 22, 123456)
To obtain only the clock time without the date, you can use:
# Get the current clock time current_time = now.time() # Print the clock time print(current_time) # Output: time(14, 35, 22, 123456)
To simplify your code, you can import the datetime object directly from the datetime module:
from datetime import datetime # Get the current datetime object now = datetime.now()
This approach eliminates the need to use the datetime. prefix when calling the datetime module functions.
The above is the detailed content of How to Get the Current Time in Python?. For more information, please follow other related articles on the PHP Chinese website!