python将分钟转化为年日小时-如何将datetime.timedelta转换为分钟,小时在Python?

I get a start_date like this:

from django.utils.timezone import utc

import datetime

start_date = datetime.datetime.utcnow().replace(tzinfo=utc)

end_date = datetime.datetime.utcnow().replace(tzinfo=utc)

duration = end_date - start_date

I get output like this:

datetime.timedelta(0, 5, 41038)

How do I convert this into normal time like:

10 minutes, 1hour like this

解决方案

There"s no built-in formatter for timedelta objects, but it"s pretty easy to do it yourself:

days, seconds = duration.days, duration.seconds

hours = days * 24 + seconds // 3600

minutes = (seconds % 3600) // 60

seconds = seconds % 60

Or, equivalently, if you"re in Python 2.7+ or 3.2+:

seconds = duration.total_seconds()

hours = seconds // 3600

minutes = (seconds % 3600) // 60

seconds = seconds % 60

Now you can print it however you want:

"{} minutes, {} hours".format(minutes, hours)

For example:

def convert_timedelta(duration):

days, seconds = duration.days, duration.seconds

hours = days * 24 + seconds // 3600

minutes = (seconds % 3600) // 60

seconds = (seconds % 60)

return hours, minutes, seconds

td = datetime.timedelta(2, 7743, 12345)

hours, minutes, seconds = convert_timedelta(td)

print "{} minutes, {} hours".format(minutes, hours)

This will print:

9 minutes, 50 hours

If you want to get "10 minutes, 1 hour" instead of "10 minutes, 1 hours", you need to do that manually too:

print "{} minute{}, {} hour{}".format(minutes, "s" if minutes != 1 else "",

hours, "s" if minutes != 1 else "")

Or you may want to write an english_plural function to do the "s" bits for you, instead of repeating yourself.

From your comments, it sounds like you actually want to keep the days separate. That"s even easier:

def convert_timedelta(duration):

days, seconds = duration.days, duration.seconds

hours = seconds // 3600

minutes = (seconds % 3600) // 60

seconds = (seconds % 60)

return days, hours, minutes, seconds

If you want to convert this to a single value to store in a database, then convert that single value back to format it, do this:

def dhms_to_seconds(days, hours, minutes, seconds):

return (((days * 24) + hours) * 60 + minutes) * 60 + seconds

def seconds_to_dhms(seconds):

days = seconds // (3600 * 24)

hours = (seconds // 3600) % 24

minutes = (seconds // 60) % 60

seconds = seconds % 60

return days, hours, minutes, seconds

So, putting it together:

def store_timedelta_in_database(thingy, duration):

seconds = dhms_to_seconds(*convert_timedelta(duration))

db.execute("INSERT INTO foo (thingy, duration) VALUES (?, ?)",

thingy, seconds)

db.commit()

def print_timedelta_from_database(thingy):

cur = db.execute("SELECT duration FROM foo WHERE thingy = ?", thingy)

seconds = int(cur.fetchone()[0])

days, hours, minutes, seconds = seconds_to_dhms(seconds)

print "{} took {} minutes, {} hours, {} days".format(thingy, minutes, hours, days)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值