gpt4 book ai didi

python - 有没有办法改变 strptime() 的阈值?

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:29 25 4
gpt4 key购买 nike

Pythonstrptime() 函数将所有年份小于 69(格式为 dd-mm-yy)的日期转换为 20XX,大于 69 的日期转换为 19XX。

是否有任何方法可以调整此设置,注意在文档中找到。

datetime.strptime('31-07-68', '%d-%m-%y').date()

datetime.date(2068, 7, 31)

datetime.strptime('31-07-68', '%d-%m-%y').date()

datetime.date(1969, 7, 31)

最佳答案

例如,我提出了将 threshold 更改为 1950-2049 的解决方案,但是您可以根据需要通过更改阈值来调整/移动它函数中的变量值:

from datetime import datetime, date

dateResult1950 = datetime.strptime('31-07-50', '%d-%m-%y').date()
dateResult2049 = datetime.strptime('31-07-49', '%d-%m-%y').date()

def changeThreshold(year, threshold=1950):
return (year-threshold)%100 + threshold

print(changeThreshold(dateResult1950.year))
print(changeThreshold(dateResult2049.year))
#1950
#2049

关于python - 有没有办法改变 strptime() 的阈值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55674151/

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