-1

In this thread we have a good solution How to calculate the time interval between two time strings

But how I can edit the output format? From H:MM:SS to HH:MM, like 00:40 instead of 0:40:00

from datetime import datetime
from datetime import timedelta

T1 = input()
T2 = input()
format = '%H:%M'
tdiff = datetime.strptime(T1, format) - datetime.strptime(T2, format)

if tdiff.days < 0:
    tdiff = timedelta(days = 0,
                seconds = tdiff.seconds, microseconds = tdiff.microseconds)

print(tdiff)

EDIT: Thank you. This code below quite works, but the only problem is 7:45 instead of 07:45 for example. The code below removes the problem of seconds at the end. Now I only don't know how to force 0 is front of <10 hours format.

from datetime import datetime
from datetime import timedelta

T1 = input()
T2 = input()
format = '%H:%M'
tdiff = datetime.strptime(T1, format) - datetime.strptime(T2, format)

if tdiff.days < 0:
    tdiff = timedelta(days = 0,
                seconds = tdiff.seconds, microseconds = tdiff.microseconds)

print(str(tdiff).rstrip("0").rstrip(":"))
Amadeus
  • 11
  • 1
  • 5
  • Possible duplicate of [Python format timedelta to string](https://stackoverflow.com/questions/538666/python-format-timedelta-to-string) – stovfl Feb 25 '19 at 21:09

1 Answers1

0

You're contradicting yourself by saying from H:MM:SS to HH:MM but you give an example of where you remove hours not seconds. Anyway, I'm guessing what you mean is that you want to remove any leading zeroes if they're empty (remove hours)?

In that case, there's two options. Either build the string yourself like so:

from datetime import datetime
from datetime import timedelta

T1 = '00:50'
T2 = '00:40'
format = '%H:%M'
tdiff = datetime.strptime(T1, format) - datetime.strptime(T2, format)

if tdiff.days < 0:
    tdiff = timedelta(days = 0, seconds = tdiff.seconds, microseconds = tdiff.microseconds)

print(':'.join([block for block in str(tdiff).split(':') if block != '0']))

Where you skip single-digit zeroes (hours) but leave double-digit zeroes (seconds etc) intact.

Or you could do:

print(str(tdiff).lstrip("0").lstrip(":"))

Which simply strips the beginning. replace with .rstrip() if you need to strip the end.

And if you want to make sure there's always a two-digit representation in the first example, you could do:

print(':'.join(['{:02}'.format(int(block)) for block in str(tdiff).split(':') if block != '0']))

At this point, you're monkey-patching a problem that most likely could be solved in a neater and more efficient way, and your original problem description is not really in line with what you're asking/trying to fix. But this is one way of doing it.

If you want to remove seconds but keep hours intact, do something like:

print(':'.join(['{:02}'.format(int(block)) for block in str(tdiff).split(':')[:-1]]))
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • Thank you, that helped much. Could you answer my edit question? – Amadeus Feb 26 '19 at 18:31
  • @Amadeus I can't reproduce the issue you're having with the Edit. For instance `00:50` and `00:45` give me `05:00` time difference. Could you give me two numbers that I can verify your issue with? – Torxed Feb 26 '19 at 19:01
  • @Amadeus I also added a way to make sure each number is padded with `0` if the length is less than 2. – Torxed Feb 26 '19 at 19:23
  • thank you. When I use your edit solution for 05:50 and 05:44 I get 06:00. For print(str(tdiff).rstrip("0").rstrip(":")) I get 0:06, and my goal is to get 00:06... very annoying stuff. Thanks in advance – Amadeus Feb 26 '19 at 20:52
  • @Amadeus When I run my code with `05:50`and `05:44` I get `06:00`. So you simply want to remove seconds all together from the equation? is that what you're asking? So you never wanted to remove the hours? Added a simple fix for removing the seconds but keeping hours and minutes intact. I'm guessing this is the last edit I'll get away with before this answer becomes super weird. – Torxed Feb 26 '19 at 21:06
  • it works perfectly! Thank you very much for your valuable help. Sorry for possible misunderstandings, but I'm not an English speaking native. =) I'm new to Python programming and have to learn a lot. Kudos to you! – Amadeus Feb 27 '19 at 08:09
  • No problem at all @Amadeus! Good luck on your project, and don't forget to mark the answer as the desired solution if it solved your problem :) – Torxed Feb 27 '19 at 09:01