Python 的 strptime()
函数将所有年份小于 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
我是一名优秀的程序员,十分优秀!