3

What I want:

I have string, like that: '1:48' I want to add some minutes to it, for example 15 minutes and to print '2:03'.

What is the problem:

I am newbie to Python and following official documentation, I cannot do it.

What I tried:

After google research I found a way to create time object from string, like this:

import time
hours = input()
minutes = input()
time_str = hours + ':' + minutes;

test = time.strptime(time_str, '%H:%M')

print(test)

But I cannot find a method from time library which add time. I found that method in datetime (timedelta) library, but there is not a method which create time object from string.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • 1
    _"but there is not a method which create time object from string."_ I don't understand. There _is_ a method, and you're using it: `time.strptime`. – Kevin Mar 08 '19 at 13:52
  • Is this a *time of day*, i.e. "1:48am", or is this a *duration*, i.e. 108 minutes? – deceze Mar 08 '19 at 13:52
  • @Kevin Cannot find hat method in `datetime` library. – MorganFreeFarm Mar 08 '19 at 13:53
  • Of course, you have *no need* for such a method, as you're already asking for hours and minutes separately; why not pass them separately to `time()`? – Daniel Roseman Mar 08 '19 at 13:53
  • @deceze It's 1:48am – MorganFreeFarm Mar 08 '19 at 13:53
  • If you're saying "there is not a method which creates a `datetime` method from string, that also exists: https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime. If you're saying "there is not a method which creates a timedelta object from string", why would you need that? What's wrong with creating it like `x = datetime.timedelta(minutes=15)`? – Kevin Mar 08 '19 at 13:54
  • 1
    Then you *should* associate it with a concrete date too. If you add a duration to a time of day, depending on local DST/timezone conditions, the answer may be very different depending on the day/the timezone. And if you do that, it's easy with a `datetime` object and a `timedelta` object. – deceze Mar 08 '19 at 13:54
  • @Kevin `Cannot find reference 'strptime' in 'datetime.pyi` – MorganFreeFarm Mar 08 '19 at 13:56
  • If you're saying "although time.strptime exists, this returns a `struct_time` object. I want an equivalent method that returns a datetime.time object. But there is no `datetime.time.strptime` method", then you could parse the string to datetime, then convert to time: `datetime.datetime.strptime("5:15", "%H:%M").time()` – Kevin Mar 08 '19 at 14:00

2 Answers2

3

You can create time objects from string of type 'hh:mm:ss' by using some string operations like:

str=input('Enter time string: ')
h,m,s=str.split(':')

The pass it to time object:

import datetime
t=datetime.time(hour=h,minute=m,second=s)

Then you can use timedelta method to add/subtract time:

td=datetime.timedelta(hour=hd,minute=MD,second=sd)
tf=t+td
kalehmann
  • 4,821
  • 6
  • 26
  • 36
  • `TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'`, at least in Python 3. You need to use `combine`. https://stackoverflow.com/a/656394/1450294 – Michael Scheper Mar 15 '22 at 20:21
2

Try this

import datetime

t = "22:00"
d = datetime.datetime.strptime(t, '%H:%M')
(d + datetime.timedelta(minutes=15)).strftime("%H:%M")
Dejan S
  • 1,445
  • 1
  • 8
  • 14