How to convert date difference from seconds to hours/days?

Asked

Viewed 4,702 times

0

I made a little program in python that he takes the current time, the time, convert in seconds and subtract from each other to know the difference between him. My doubt is, how do I convert these seconds to hours and days?

Code that converts the date to seconds:

from datetime import date, datetime
import time

date_now = time.mktime(datetime.now().timetuple())
date_created = time.mktime(get_demand_if_exist[0].date_created.timetuple())
diff_time = abs(date_now - date_created)

2 answers

1


This is the code that asks the user for the number of seconds and displays in days, hours, minutes and the remaining seconds.

Just vc put your output in seconds instead of the variable seconds and ready.

But remember to convert your output to whole.

segundos = int(input("Segundos: "))

dias = segundos // 86400
segundos_rest = segundos % 86400
horas = segundos_rest // 3600
segundos_rest = segundos_rest % 3600
minutos = segundos_rest // 60
segundos_rest = segundos_rest % 60

print(dias,"dias,",horas,"horas,",minutos,"minutos e",segundos_rest,"segundos.")
  • 1

    Exactly what I needed, because I just needed to calculate the days/hours with the seconds I could. Thank you!

  • It is better to use a subtraction of two objects datetime, that in Python creates an objto timedelta - see the other answer. (The package arrow is unnecessary, in fact). This answer is good if you already have the diffraction of fear between seconds - but if you have dates, Python datetime will already count months and years with variable duration, etc...

  • 1

    @jsbueno, I explained the use of Arrow in the comets of my answer, my example would be given without Arrow, but datetime required that the first date of the example would have to be "complete" (including seconds), as I don’t know where the date he was getting would come from (database?) I thought Arrow would make it easier. Arrow is a 10. :-)

1

TL;DR*

The question seems to be truncated, but it seems to indicate that you want to know the difference between the current date and the date of creation of something, ie the difference between two dates:

Arrow

$ pip install arrow

Creating the dates:

start = arrow.get(datetime.date(2017, 8, 16))
end = arrow.get(datetime.datetime.now())

Calculating the delta between the two:

td = end - start 

Investigating the timedelta:

# Resultado
print (td)
7 days, 21:21:36.785675

# Número de dias:
print (td.days)
7

# Número de segundos
print (td.seconds)
76896

# Número de horas:
print (td.seconds/3600)
21.36

# Mais refinado (Número total de segundos)
td.total_seconds()
682433.962907

See running on repl.it.*

* Results may be discrepant compared to code running on local machine depending on time zone

  • In fact, the package arrow is unnecessary for the simplest cases - subtracting one datetime object from another directly produces one object timedelta in the same way. What Arrow does is allow there is the subtraction between a "datetime" and a "date" - if two objects are used datetime your code even works the same way.

  • I always recommend the Arrow because it fails to mention, especially for those who are starting, create a Delta without it can become a nightmare for those who have no intimacy with Datetime, in this example I gave would no longer work.

  • 1

    In fact, I was going to set an example without Arrow, but I gave up when I saw trouble. :-)

Browser other questions tagged

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