AttributeError: 'datetime.timedelta' object has no attribute 'split'
AttributeError: 'datetime.timedelta' object has no attribute 'split'
我正在尝试通过以下代码获取与日期时间相差的小时数
'''
from datetime import datetime
from datetime import date
from datetime import time
from datetime import timedelta
import sys
a = date.today()
b = datetime.now()
print("Today is :" +str(b))
q = int(input("enter your birth month :"))
w = int(input("enter your brth day :"))
my_bday = date(a.year,q,w)
#print(my_bday)
if my_bday < a :
my_bday = my_bday.replace(year = a.year +1)
time_to_bday = abs (a -my_bday)
#print("The number of days remaining for your bday is :" +time_to_bday)
o = time_to_bday.split(' ')
z = o[0]
k = timedelta(days = z)
print("Time remaining for your bday is : " +str(b + k))
此处拆分不起作用并出现以下错误:
Traceback (most recent call last):
File "f:/Ch2/practise.py", line 96, in <module>
o = time_to_bday.split(' ')
AttributeError: 'datetime.timedelta' object has no attribute 'split'
我仔细检查了代码 o/p 直到 time_to_bday 如下:
PS F:\Ch2> & "C:/Users/sai kiran/AppData/Local/Programs/Python/Python36-32/python.exe" f:/Ch2/practise.py
Today is :2021-05-01 20:32:08.677781
enter your birth month :11
enter your brth day :17
200 days, 0:00:00
Timedelta
不是 str
,所以没有 spilit()
。
print()
将其转换为 str
。
例如,您需要调用 o=str(o)
你已经及时计算了delta,它不再是字符串了
time_to_bday是timedelta对象,可以直接获取days如下
time_to_bday.days
如果您想按小时打印,您可以使用 time_to_bday.days * 24
打印
From your code ::
enter your birth month :4
enter your brth day :17
>> time_to_bday.days
350
我正在尝试通过以下代码获取与日期时间相差的小时数 '''
from datetime import datetime
from datetime import date
from datetime import time
from datetime import timedelta
import sys
a = date.today()
b = datetime.now()
print("Today is :" +str(b))
q = int(input("enter your birth month :"))
w = int(input("enter your brth day :"))
my_bday = date(a.year,q,w)
#print(my_bday)
if my_bday < a :
my_bday = my_bday.replace(year = a.year +1)
time_to_bday = abs (a -my_bday)
#print("The number of days remaining for your bday is :" +time_to_bday)
o = time_to_bday.split(' ')
z = o[0]
k = timedelta(days = z)
print("Time remaining for your bday is : " +str(b + k))
此处拆分不起作用并出现以下错误:
Traceback (most recent call last):
File "f:/Ch2/practise.py", line 96, in <module>
o = time_to_bday.split(' ')
AttributeError: 'datetime.timedelta' object has no attribute 'split'
我仔细检查了代码 o/p 直到 time_to_bday 如下:
PS F:\Ch2> & "C:/Users/sai kiran/AppData/Local/Programs/Python/Python36-32/python.exe" f:/Ch2/practise.py
Today is :2021-05-01 20:32:08.677781
enter your birth month :11
enter your brth day :17
200 days, 0:00:00
Timedelta
不是 str
,所以没有 spilit()
。
print()
将其转换为 str
。
例如,您需要调用 o=str(o)
你已经及时计算了delta,它不再是字符串了
time_to_bday是timedelta对象,可以直接获取days如下
time_to_bday.days
如果您想按小时打印,您可以使用 time_to_bday.days * 24
打印From your code ::
enter your birth month :4
enter your brth day :17
>> time_to_bday.days
350