Seconds ==> HH:MM:SS

  1. sec = 6010
  2. # 方法1
  3. from datetime import timedelta
  4. td = timedelta(seconds=sec)
  5. str(td)
  6. # 方法2
  7. hours = sec // 3600 % 24
  8. minutes = sec // 60 % 60
  9. seconds = sec % 60
  10. td = f'{hours}:{minutes}:{seconds}'

HH:MM:SS ==> Seconds

  1. td = '1:40:10'
  2. # 方法1
  3. hours, minutes, seconds = map(int, td.split(':'))
  4. sec = hours * 3600 + minutes * 60 + seconds
  5. # 方法2
  6. sec = timedelta(hours=hours, minutes=minutes, seconds=seconds).seconds