Passing string to team

Asked

Viewed 478 times

1

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

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

Is there any way I can "assemble" a time-type variable with these variables of mine hora_inicio, minuto_inicio to make it easier to calculate the time spent?

  • How will you calculate the interval in days if the strings only talk about hours, minutes and seconds?

  • @Pabloalmeida days are sent directly by other input

  • You just want to know how to create datetimes for those hours?

  • exact. I want to know how to use hh to compose the one referring to the hour, mm to the field referring to the seconds, etc.

  • 2

    As an observation, you don’t need to split; just use the datetime.strptime.

1 answer

2


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. As you want the difference between two dates, use subtraction:

diferenca = dataComHora2 - dataComHora1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.