首页 > 解决方案 > Python字符串到日期时间日期

问题描述

我有很多这样的日期:(16.8.18美国:2018 年 8 月 16 日)字符串类型。现在,我需要检查日期是过去还是将来,但datetime不支持德语格式。

我怎样才能做到这一点?

标签: python-3.xdatetime

解决方案


from datetime import datetime

s = "16.8.18"
d = datetime.strptime(s, "%d.%m.%y")

if d > datetime.now():
    print('Date is in the future.')
else:
    print('Date is in the past.')

打印(今天是 20.7.2018):

Date is in the future.

使用的格式在手册页strptime()中有解释。


推荐阅读