gpt4 book ai didi

python - 在 python 错误中舍入时间

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:20 28 4
gpt4 key购买 nike

我在尝试汇总时间时遇到以下错误,

AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

fajr_jamaat1 打印输出为 1900-01-01 04:25:00请帮忙?

from datetime import datetime, timedelta

def round_up(tm):
upmins = math.ceil(float(tm.minute)/10)*10
diffmins = upmins - tm.minute
newtime = tm + datetime.timedelta(minutes=diffmins)
newtime = newtime.replace(second=0)
return newtime

cnx = mysql.connector.connect(host=mysql_remote_host, user=mysql_remote_host_user, password=mysql_remote_host_password, database=mysql_remote_host_database)
cursor = cnx.cursor()
cursor.execute("select * from paramatta_prayertimes WHERE id =1")
results = cursor.fetchall()
id, date, fajr_begins, fajr_jamaat, sunrise, zuhr_begins, zuhr_jamaat, asr_begins, asr_jamaat, maghrib_jamaat, isha_begins, isha_jamaat = results[0]
cursor.close()
print fajr_begins

fajr_jamaat1=(timedelta(minutes=20) + datetime.strptime(str(fajr_begins), "%H:%M:%S"))
print fajr_jamaat1
round_up(fajr_jamaat1)

最佳答案

因为您将函数导入为

from datetime import datetime, timedelta

你不需要像这样调用函数

datetime.timedelta

你只需通过函数名调用函数

timedelta

如果您更喜欢使用前一种语法,只需导入模块本身

import datetime

然后你可以说

datetime.datetime
datetime.timedelta

关于python - 在 python 错误中舍入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036600/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com