gpt4 book ai didi

python - 使用python从mysql中获取时间

转载 作者:行者123 更新时间:2023-11-30 22:59:13 25 4
gpt4 key购买 nike

我正在使用下面的代码从 mysql 中获取名为 fajr_begins 的时间实体。我能够打印格式为:5:27:00 的 fajr_begins,如何将其转换为 python 中的时间格式,以便我可以操纵时间并添加 15 分钟?

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 prayertimes where DATE(date) = DATE(NOW())" )
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

最佳答案

你必须使用 strptime 来转换它. timedelta 将用于添加分钟。

>>> from datetime import datetime, timedelta
>>> a = "05:27:00"
>>> b = datetime.strptime(a, "%H:%M:%S")
>>> print b
1900-01-01 05:27:00
>>> c = timedelta(minutes=15)
>>> print c
0:15:00
>>> print b+c
1900-01-01 05:42:00

关于python - 使用python从mysql中获取时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803650/

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