Reputation: 21
How do I add the hour,min, and second to the time that I have created?
Code:
import datetime
time_entry = input('Enter a time in hh:mm:ss format:')
hours, minutes, second = map(int, time_entry.split(':'))
t = datetime.time(hours, minutes, second)
h = int(input('Hours:'))
m = int(input('Minutes:'))
s = int(input('Seconds:'))
print('Current time is:',t)
t1 = datetime.time(hours + h, minutes + m, second + s)
print('New time is:',t1)
i want it to come out like this:
Enter a time in hh:mm:ss format:23:23:23
Hours:2
Minutes:2
Seconds:2
Current time is: 23:23:23
New time is: 1:25:25
but I got:
t1 = datetime.time(hours + h, minutes + m, second + s)
ValueError: hour must be in 0..23
Upvotes: 2
Views: 8688
Reputation: 76912
You want to use timedelta
object:
from datetime import timedelta
t1 = datetime.time(hours, minutes, second) + timedelta(seconds=s, minutes=m, hours=h)
and in there s
and m
can be greater than 59, and h
greater 23 without problem
Upvotes: 2