Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add minutes to datatime.time. Python/Pandas [duplicate]

I have a datetime.time object:

A = datetime.time(15, 50)

How can I add 10 minutes so that :

B = A + (10min)
B = datetime.time(16, 0)

Thanks

Edit: Question is not a dup. It involves the transformation from date.time to datetime.datetime as shown by the answer.

like image 327
hernanavella Avatar asked Mar 10 '23 23:03

hernanavella


1 Answers

You will need to transform your time into a fully fledged datetime.datetime before you can add your ten minutes

def add_delta(tme, delta):
    # transform to a full datetime first
    return (datetime.datetime.combine(datetime.date.today(), tme) + 
            delta).time()

Then

>>> add_delta(datetime.time(15, 50), datetime.timedelta(minutes=10))
datetime.time(16, 0)

This will roll times according to clock arithmetic.

>>> add_delta(datetime.time(23, 55), datetime.timedelta(minutes=10))
datetime.time(0, 5)
like image 62
donkopotamus Avatar answered Mar 23 '23 00:03

donkopotamus