Home > Net >  Python: How to convert time.time() to minutes and seconds which is compatible with Flask
Python: How to convert time.time() to minutes and seconds which is compatible with Flask

Time:07-27

I am currently trying to output a time to flask, this needs to be displayed in minutes and seconds, an example being:

2:14

I tried using the datetime/timedelta module but it does not appear to work with flask, here is a snippet of my code

 if (len(red_circles[0, :]) == 7):
                start_time = time.time()


            else:
                if len(red_circles[0, :]) == 0:
                    end_time = time.time()
                    time_taken = end_time - start_time

                    times.append(time_taken)
                    average = sum(times) / len(times) / 60

Any help would be appreciated!

CodePudding user response:

I'm not sure why you can't get it to work with flask, seems like it might be a variable misnamed but here's a solution I've tested on my machine printing to terminal and returning from a flask route. It uses datetime.now() because the formatting is a bit easier to split/slice.

from datetime import datetime
import time

start = datetime.now()
time.sleep(5)
end = datetime.now()
duration = end - start

duration_in_minutes_and_seconds = str(duration).split('.')[0][2:]

print(duration_in_minutes_and_seconds)
  • Related