Is there a method where you get seconds and calculate them in hours:minutes:seconds?

Asked 2 years ago, Updated 2 years ago, 20 views

In the team project, you need to get the return value of the function that someone else planned as a factor and do something else.

The problem is that all of these functions return in seconds, and my functions need to be printed in hours, minutes, and seconds. It's hard to fix the sauce now.

I haven't dealt with time-related modules in Python much, but I know that everything is implemented, so is there a function like this?

python

2022-09-22 22:12

1 Answers

Write datetime.timedelta

import datetime
mydelta = datetime.timedelta(seconds=666)
mytime = datetime.datetime.min + mydelta #datetime.datetime is calculated by year/month/day, so h over 24 hours is converted to day.
h, m, s = mytime.hour, mytime.minute, mytime.second

Use divmod() to prevent 24 hours from converting to 1 day. You need to process the results of the mathematical calculations using the quotient and the remainder.

m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "%d:%02d:%02d" % (h, m, s)


2022-09-22 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.