Passing string to time

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

I have already split and have the fields all separated into their own integer type variables, for example: hora_inicio, minuto_inicio, etc. However, I am having problem for the conversions minutes X hours, seconds x minutes, etc.

Has there any way I can "assemble" a variable of Type time with these my variables hora_inicio, minuto_inicio to so get easier the calculation of the past time?

Author: Maniero, 2018-01-29

1 answers

Use the Class datetime 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 mathematical operations on objects datetime. 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.)

 2
Author: Pablo Almeida, 2018-01-29 22:55:20