【问题标题】:Python timedelta class minutes [duplicate]Python timedelta类分钟[重复]
【发布时间】:2020-07-28 16:40:21
【问题描述】:

如何获取 timedelta 的分钟和秒数(例如:2:01)?这个有公式吗?

这个我试过了,但是真的不好用

minutes = int(diff.seconds/60)

【问题讨论】:

    标签: python python-3.x python-3.6 timedelta


    【解决方案1】:

    Isayahs 的回答效果很好,但如果您不想使用任何库,您可以执行类似的操作:

    minutes = seconds // 60
    remainingSeconds = seconds % 60
    print(str(minutes) + ":" + str(remainingSeconds))
    

    【讨论】:

      【解决方案2】:

      我可能误解了这个问题,但如果您正在寻找 timedelta 对象,而 seconds = 121 则:

      >>> from datetime import timedelta
      >>> timedelta(minutes=2, seconds=1)
      datetime.timedelta(seconds=121)
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-21
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2011-01-08
        • 1970-01-01
        相关资源
        最近更新 更多