Junior — Middle
What methods or libraries can you use to calculate date and time after a specified number of hours?
sobes.tech AI
Answer from AI
To calculate the date and time after a specified number of hours in Python, the datetime module is usually used. The main approach is to add a timedelta object, which represents a time interval, to the current time.
Example:
from datetime import datetime, timedelta
# Current time
now = datetime.now()
# Add 5 hours
future_time = now + timedelta(hours=5)
print(future_time)
You can also use third-party libraries, such as pytz for working with time zones or dateutil for more flexible date and time handling, but for simple tasks, the standard datetime module is sufficient.