Python从秒中获取小时数和天数

在使用我的代码的 python 中,我从秒中得到最小值。但我也想要 60 分钟后的小时数和 24 小时后的天数 我怎样才能使这成为可能 任何人都知道吗?


我的代码如下:


time = int(booking[index]['fields']['google_time'])

time_in_min = time / 60

booking[index]['fields']['google_time_in_min'] = round(time_in_min)


慕码人2483693
浏览 96回答 3
3回答

犯罪嫌疑人X

从您的问题中不清楚您希望以哪种格式获得结果,但您可以使用递归函数轻松解决您的问题。我总是会在几秒钟内节省时间,然后再进行转换。你可以添加天,月,年......def transform_time(sec):&nbsp; &nbsp; if sec < 60:&nbsp; &nbsp; &nbsp; &nbsp; return str(sec) + ' sec'&nbsp; &nbsp; if 60 < sec < 60*60:&nbsp; &nbsp; &nbsp; &nbsp; return str(round(sec/60)) + 'mins :' + str(transform_time(sec % 60))&nbsp; &nbsp; if 60*60 < sec < 24*60*60:&nbsp; &nbsp; &nbsp; &nbsp; return str(round(sec/60)) + 'hours :' + transform_time(sec)if __name__ == '__main__':&nbsp; &nbsp; print(transform_time(3032))

智慧大石

怎么样:def work_out_hour_min_sec(total_seconds):&nbsp; &nbsp; hours = int(total_seconds / 3600)&nbsp; &nbsp; mins = int(total_seconds % 3600 / 60)&nbsp; &nbsp; seconds = total_seconds - hours * 3600 - mins * 60&nbsp; &nbsp; return hours, mins, secondsprint(work_out_hour_min_sec(3700))可能不是最蟒蛇/优雅的方式,但只需 5 分钟的思考。

噜噜哒

这很容易)time_in_min = time / 60time_in_hours = ''time_in_days = ''if time_in_min == 60:&nbsp; &nbsp; time_in_min = 0&nbsp; &nbsp; time_in_hours = 1if time_in_hours == 24:&nbsp; &nbsp; time_in_hours = 0&nbsp; &nbsp; time_in_days = 1等等
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python