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

1

I made a program in python that it takes the current time, time, convert in seconds and subtract one from the other to know the difference between it. My question is, how do I convert those seconds to hours and days?

Code that converts 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)
    
asked by anonymous 24.08.2017 / 00:49

2 answers

1

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

Just put your output in seconds instead of the second variable and that's it.

But remember to convert your output to integer.

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.")
    
24.08.2017 / 01:15
2

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 creation date of something, that is, 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 timed tape:

# 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

View running on repl.it.

* The results may be inconsistent compared to the code running on the local machine depending on the time zone

    
24.08.2017 / 02:28