扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Django将秒转换为xx天xx时xx分,具体代码如下所示:
from django.utils.translation import ngettext_lazy as _n def humanize_seconds(secs): a_day = 86400 an_hour = 3600 a_minute = 60 timetot = '' total_secs = secs if secs > a_day: # 60sec * 60min * 24hrs days = int(secs // a_day) # timetot += "{} {}".format(int(days), _('days')) timetot += _n('%(num)s day', '%(num)s days', days) % {'num': days} secs = secs - days * a_day if secs > an_hour: hrs = int(secs // an_hour) # timetot += " {} {}".format(int(hrs), _('hours')) timetot += ' ' timetot += _n('%(num)s hour', '%(num)s hours', hrs) % {'num': hrs} secs = secs - hrs * an_hour if secs > a_minute and total_secs < a_day: mins = int(secs // a_minute) timetot += ' ' timetot += _n('%(num)s minute', '%(num)s minutes', mins) % {'num': mins} secs = secs - mins * a_minute if secs > 0 and total_secs < an_hour: secs = int(secs) timetot += ' ' timetot += _n('%(num)s second', '%(num)s seconds', secs) % {'num': secs} return timetot if __name__ == "__main__": print(humanize_seconds(360200))
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流