如何将datetime.timedelta转换为分钟,小时在Python? [英] How to convert datetime.timedelta to minutes, hours in Python?
问题描述
我得到一个这样的start_date:
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
我得到如下输出:
datetime.timedelta(0,5,41038)
我会把它转换成正常的时间,如:
10分钟,1小时这样
对于 timedelta
对象,没有内置的格式化程序,但是很容易自己做:
days,seconds = duration.days,duration.seconds
hours = days * 24 + seconds // 3600
minutes =(seconds%3600)// 60
秒=秒%60
或者,如果你在Python 2.7+或3.2 +:
秒= duration.total_seconds()
hours = seconds // 3600
minutes =(seconds%3600)// 60
秒=秒%60
现在可以打印,但是你想要的:
{}分钟,{}小时。格式(分钟,小时)
例如: / p>
def convert_timedelta(duration):
days,seconds = duration.days,duration.seconds
hours =天* 24 +秒// 3600
分钟=(秒%3600)// 60
秒=(秒%60)
返回小时,分钟,秒
td = datetime
小时,分钟,秒= convert_timedelta(td)
打印'{}分钟,{}小时'格式(分钟,小时)
这将打印:
9分钟,50小时
如果要10分钟,1小时而不是 10分钟,1小时,您需要手动执行:
打印'{}分钟{},{}小时{}'格式(分钟,'如果分钟!= 1其他' ,
小时,'如果分钟!= 1其他'')
或者你可能要为您写一个 english_plural
函数来为您执行''
位,而不是重复自己。 p>
从你的评论来看,这听起来像是你真的想把这些天分开。这更容易:
def convert_timedelta(duration):
days,seconds = duration.days,duration.seconds
小时=秒// 3600
分钟=(秒%3600)// 60
秒=(秒%60)
返回天数,小时,分钟,秒
如果要将其转换为单个值以存储在数据库中,则将该单个值转换为格式化,这样做:
def dhms_to_seconds(天,小时,分钟,秒):
return(((days * 24)+小时)* 60 +分钟)* 60 +秒
def seconds_to_dhms(秒):
天=秒//(3600 * 24)
小时=(秒// 3600)%24
分钟=(秒// 60)%60
秒=秒%60
返回天数,小时,分钟,秒
所以,把它放在一起:
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])
天,小时,分钟,秒=秒_to_dhms(秒)
打印'{}花费{}分钟,{}小时,{}天。格式(东西,分钟,天)
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)
这篇关于如何将datetime.timedelta转换为分钟,小时在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!