Python datetime isoformat-从字符串计算bithday之前的天数-转换为datetime.datetime对象

Python datetime isoformat-从字符串计算bithday之前的天数-转换为datetime.datetime对象,python,python-datetime,Python,Python Datetime,我想数到生日的天数。我有带出生日期的字符串: "1949-10-09T00:25:51.304Z" 及 返回 2020-08-05T21:02:31.532123 <class 'str'> 我是否必须更改今天的字符串片段才能获得: "2020-08-05T21:02:31.532" 然后连接Z以获得: "2020-08-05T21:02:31.532Z" 然后使用datetime.strTime将其转换为对象, 并

我想数到生日的天数。我有带出生日期的字符串:

"1949-10-09T00:25:51.304Z"

返回

2020-08-05T21:02:31.532123
<class 'str'>
我是否必须更改今天的字符串片段才能获得:

"2020-08-05T21:02:31.532"
然后连接Z以获得:

"2020-08-05T21:02:31.532Z"
然后使用datetime.strTime将其转换为对象, 并将生日日期也转换为对象日期,使用这个

birthday = datetime.fromisoformat('1949-10-09T00:25:51.304Z')
还是有更聪明的方法

要获取生日字符串,我必须使用解析JSON文件的函数

date=get_double_nested_table_data(0, "dob", "date")
birthday = datetime.fromisoformat(date)
now = datetime.now()
delta = now - birthday
它不起作用

我用下面的答案来回答:

def get_days_until_birthday(index: int, first_table: str, second_table: str):
    birthday_str = get_double_nested_table_data(index, first_table, second_table)
    birthday = datetime.strptime(birthday_str, "%Y-%m-%dT%H:%M:%S.%fZ")
    now = datetime.now()
    delta = now - birthday
    return delta.days
这将返回这样的行:

我将该函数改为:

def get_days_until_birthday(index: int, first_table: str, second_table: str):
    now = datetime.now()
    year = now.year
    birthday_str = get_double_nested_table_data(index, first_table, second_table)
    birthday_str = str(year) + birthday_str[4:]
    birthday = datetime.strptime(birthday_str, "%Y-%m-%dT%H:%M:%S.%fZ")
    now = datetime.now()
    delta = now - birthday
    return delta.days
只有在今年生日到来时,这才行。如果生日在下一年,该函数不会返回正确的天数


您知道如何更改吗?

您想要的是减去两个日期时间,这两个日期时间之间的时间差

from datetime import datetime

birthday_str = '1949-10-09T00:25:51.304Z'
# you can remove the Z by slicing
birthday = datetime.fromisoformat(birthday_str[:-1])
# or with strptime
birthday = datetime.strptime(birthday_str, "%Y-%m-%dT%H:%M:%S.%fZ")
now = datetime.now()
delta = now - birthday

# return the number of days (positive) there is between now and birthday date
return (now - birthday).days if birthday < now else (birthday - now).days
strtime格式:

%Y是4位数的年份* %m*以2位数字表示的月份 %d以两位数字表示一天 T以仍然匹配您正在使用的字符串的格式 %小时到了 :以仍与正在使用的字符串的格式匹配 %M是会议记录 %S代表秒 . 仍然匹配您正在使用的字符串的格式 %f表示微秒
Z仍然匹配您正在使用的字符串的格式

该代码返回错误:ValueError:无效的isoformat字符串:“1966-06-26T11:50:25.558Z”再次返回错误。我想我的生日字符串与isoformat不匹配。生日字符串在点后没有那么多数字。我想这是Z的背后,我编辑了我的代码,让它使用strTimeNice为你工作!你能接受我的回答吗,因为它解决了你的问题=我错了,数据库中的结果是例如19764、25868。。。
def get_days_until_birthday(index: int, first_table: str, second_table: str):
    now = datetime.now()
    year = now.year
    birthday_str = get_double_nested_table_data(index, first_table, second_table)
    birthday_str = str(year) + birthday_str[4:]
    birthday = datetime.strptime(birthday_str, "%Y-%m-%dT%H:%M:%S.%fZ")
    now = datetime.now()
    delta = now - birthday
    return delta.days
from datetime import datetime

birthday_str = '1949-10-09T00:25:51.304Z'
# you can remove the Z by slicing
birthday = datetime.fromisoformat(birthday_str[:-1])
# or with strptime
birthday = datetime.strptime(birthday_str, "%Y-%m-%dT%H:%M:%S.%fZ")
now = datetime.now()
delta = now - birthday

# return the number of days (positive) there is between now and birthday date
return (now - birthday).days if birthday < now else (birthday - now).days