Passing string to time

1

I'm getting 2 strings in hh: mm: ss format to calculate the time passed between them in days, hours, minutes, and seconds format.

I have done the split and I have the fields all separated in their own integer variables, for example: hora_inicio, minuto_inicio , etc. However, I'm having trouble converting minutes x hours, seconds x minutes, etc.

Is there any way I can "mount" a variable of type type with these my variables hora_inicio, minuto_inicio to then make it easier to calculate past time?     

asked by anonymous 29.01.2018 / 23:02

1 answer

2

Use the datetime class of the datetime package to create a date with time information :

from datetime import datetime

dataComHora1 = datetime(2018, 1, 29, 22, 35, 12)

You can do math operations on datetime objects. Since you want the difference between two dates, use the subtraction:

diferenca = dataComHora2 - dataComHora1

With this resulting object, which is of type timedelta , you can extract the information you want ( seconds , minutes , hours , total_seconds etc.)

    
29.01.2018 / 23:55