How do I get the current time?

To get the current time in Python, you can use the datetime module from the standard library.

Here is an example of how you can get the current time and print it in a human-readable format:

import datetime

now = datetime.datetime.now()
print(now)

The datetime.datetime.now() function returns a datetime object that represents the current date and time.

Watch a course Python - The Practical Guide

You can then use various methods of the datetime object to extract the information you need, such as the year, month, day, hour, minute, and second.

For example, to print only the current hour, you can use the following code:

import datetime

now = datetime.datetime.now()
print(now.hour)

You can also use the strftime method of the datetime object to format the date and time in a specific way. For example, to print the current date and time in the format "YYYY-MM-DD HH:MM:SS", you can use the following code:

import datetime

now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))