-1

I have a coding assignment where we are supposed to find the difference between two timestamps. I have completed the code for that however it is supposed to print as "x hour(s) y minute(s) z second(s)".

I tried using print('hours" + (x), 'minutes' + (y), 'seconds' + (z) but it is not working.

x = int(((a-b) // 3600)) y = int(((a-b) % 3600) // 60) z = int((a-b) % 60 )

this is the code I am using.

marcelovca90
  • 2,673
  • 3
  • 27
  • 34
enalb
  • 11
  • 3

2 Answers2

2

Assuming you are using python 3.*

a = 1
b = 2

x = int(((a-b) // 3600))
y = int(((a-b) % 3600) // 60)
z = int((a-b) % 60 )

print(f'hours {x} minutes {y} seconds {z}')
Madison Courto
  • 1,231
  • 13
  • 25
0

It would make sense to only do the subtraction and the integer conversion once.

from datetime import datetime

a = datetime.now().timestamp()
b = datetime(2019,9,1).timestamp()

c = int(a - b)

hh = c // 3600
mm = c % 3600 // 60
ss = c % 60

print(f"hours: {hh}, minutes: {mm}, seconds: {ss}")

Result

hours: 213, minutes: 10, seconds: 27
Deepstop
  • 3,627
  • 2
  • 8
  • 21