Python datetime isoformat - 从字符串计算生日前的天数 - 转换为 datetime.datetime 对象
Python datetime isoformat - count days until bithday from string - convert into datetime.datetime object
我想数到生日的天数。我有出生日期的字符串:
"1949-10-09T00:25:51.304Z"
和
tday = datetime.now().isoformat()
那个returns
2020-08-05T21:02:31.532123
<class 'str'>
我想知道如何创建这两个字符串的增量时间。
我看到了这些字符串之间的区别:
"1949-10-09T00:25:51.304Z" # date of birth
"2020-08-05T21:02:31.532123" # today
我是否必须更改“今天”字符串(切片得到:
"2020-08-05T21:02:31.532"
然后连接“Z”得到:
"2020-08-05T21:02:31.532Z"
然后使用 datetime.strptime() 将其转换为对象,
并将生日日期也转换为对象日期,使用这个?
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
和 return 行是这样的:
weird ints
我把那个函数改成了这个:
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
而且只有在今年生日的情况下才能正常工作。如果明年生日,该函数不会 return 更正日期。
你知道怎么改吗?
你想要的是减去两个日期时间,得到它们之间的时间差
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
strptime 格式reference:
%Y 是4 dig中的年份*
%m* 2 位数的月份
%d 2 位数的日期
T 仍然匹配您正在使用的字符串的格式
%H 是小时
: 仍然匹配您正在使用的字符串的格式
%M是分钟
%S 秒
. 仍然匹配您正在使用的字符串的格式
%f 为微秒
Z 仍然匹配您正在使用的字符串的格式
我想数到生日的天数。我有出生日期的字符串:
"1949-10-09T00:25:51.304Z"
和
tday = datetime.now().isoformat()
那个returns
2020-08-05T21:02:31.532123
<class 'str'>
我想知道如何创建这两个字符串的增量时间。
我看到了这些字符串之间的区别:
"1949-10-09T00:25:51.304Z" # date of birth
"2020-08-05T21:02:31.532123" # today
我是否必须更改“今天”字符串(切片得到:
"2020-08-05T21:02:31.532"
然后连接“Z”得到:
"2020-08-05T21:02:31.532Z"
然后使用 datetime.strptime() 将其转换为对象, 并将生日日期也转换为对象日期,使用这个?
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
和 return 行是这样的: weird ints
我把那个函数改成了这个:
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
而且只有在今年生日的情况下才能正常工作。如果明年生日,该函数不会 return 更正日期。
你知道怎么改吗?
你想要的是减去两个日期时间,得到它们之间的时间差
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
strptime 格式reference:
%Y 是4 dig中的年份*
%m* 2 位数的月份
%d 2 位数的日期
T 仍然匹配您正在使用的字符串的格式
%H 是小时
: 仍然匹配您正在使用的字符串的格式
%M是分钟
%S 秒
. 仍然匹配您正在使用的字符串的格式
%f 为微秒
Z 仍然匹配您正在使用的字符串的格式