I want to do something like 2h35 + 0h56
in python. So this is what I tried :
>>> t1 = time[2, 35]
>>> t2 = time[0, 56]
>>> t3 = t1 + t2
Traceback [most recent call last]:
File "", line 1, in
TypeError: unsupported operand type[s] for +: 'datetime.time' and 'datetime.time'
How can I add times on a time in Python ? All the subjects on Stackoverflow ask for add times on a date, but I just need time no date!
Thanks for your help
asked Sep 18, 2015 at 20:51
Louis EtienneLouis Etienne
1,2623 gold badges20 silver badges37 bronze badges
2
from datetime import datetime
from dateutil.relativedelta import relativedelta
if __name__ == '__main__':
t1 = datetime[year=2015, day=19, month=9, hour=2, minute=35]
t2 = t1 + relativedelta[minutes=56]
print[t2.strftime['%H:%m']]
You must specify a real date, but in your case, you are only interested in the hours and minutes, so you can just print them using strftime.
answered Sep 18, 2015 at 21:55
DevLoungeDevLounge
8,2473 gold badges30 silver badges43 bronze badges
1
datetime.time
represents absolute times and it doesn't make any sense to add e.g. 2:35 + 0:56
.
See python time + timedelta equivalent. The solution there might help you.
answered Sep 18, 2015 at 21:16
cg909cg909
2,01720 silver badges22 bronze badges
Prerequisites: Datetime module
Every minute should be enjoyed and savored. Time is measured by the hours, days, years, and so on. Time helps us to make a good habit of organizing and structuring our daily activities. In this article, we will see how we can extract real-time from a python module. There are various ways to pass the date and time feature to the program. Python ‘Time’ and ‘Calendar’ module help in tracking date and time. Also, the ‘DateTime’ provides a class for controlling date and time in both simple and complex ways. So with the help of this module, we will try to figure out our future desire time by adding hours in real-time with the help of ‘timedelta[ ]’.
To get both current date and time datetime.now[] function of DateTime module is used. This function returns the current local date and time.
Syntax : datetime.now[tz]
Parameters : tz : Specified time zone of which current time and date is required. [Uses Greenwich Meridian time by default.]
Returns : Returns the current date and time in time format.
Approach :
- Import DateTime module.
- Display Current time.
- Create a new variable to Updated time.
- Store the Updated time in that variable.
- Display Updated Time.
Implementation :
Step 1: Showing current time.
Firstly, we will Import ‘datetime’ and ‘timedelta’ from datetime module, Then we will store our Present time in a variable. After that, we will align date in “HH:MM:SS” format. Now we can print our Present time.
Python3
from
datetime
import
datetime, timedelta
present_time
=
datetime.now[]
'{:%H:%M:%S}'
.
format
[present_time]
print
[
"Present time at greenwich meridian is "
,end
=
""]
print
[ present_time ]
Output:
Present time at greenwich meridian is 2020-11-11 08:26:55.032586
Step 2: Adding the time to the existing current time.
After the following above steps, we will pass the desired time in ‘timedelta’ function that will add hour in present time.
Now, we can display updated time.
Python3
from
datetime
import
datetime, timedelta
present_time
=
datetime.now[]
'{:%H:%M:%S}'
.
format
[ present_time ]
print
[
"Present time at greenwich meridian is "
,
end
=
""]
print
[ present_time ]
updated_time
=
datetime.now[]
+
timedelta[hours
=
6
]
print
[ updated_time ]
Output:
Present time at greenwich meridian is 2020-11-11 08:27:39.615794
2020-11-11 14:27:39.615794
Another better way you can try :
Python3
from
datetime
import
datetime, timedelta
updated
=
[ datetime.now[]
+
timedelta[ hours
=
5
]].strftime[
'%H:%M:%S'
]
print
[ updated ]
Output:
13:28:21