To get the current time in Python, you can use the datetime
module. Here’s an example:
import datetime
now = datetime.datetime.now()
print(“Current date and time: “)
print(now.strftime(“%Y-%m-%d %H:%M:%S”))
This will output the current date and time in the format YYYY-MM-DD HH:MM:SS
, like this:
2022-03-02 11:07:23
In the example above, we first import the datetime
module. Then we call the now()
function to get the current date and time as a datetime
object. Finally, we use the strftime()
method to format the date and time as a string that can be printed to the console.
You can customize the format string passed to strftime()
to display the date and time in different formats. The format codes used in the string are documented in the Python documentation.
- sunny asked 1 month ago
- You must login to post comments
Please login first to submit.