代码之家  ›  专栏  ›  技术社区  ›  anon

Python字符串到日期时间日期

  •  2
  • anon  · 技术社区  · 6 年前

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

    我怎样才能做到这一点?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Andrej Kesely    6 年前
    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.')
    

    印刷品(今天是2018年7月20日):

    Date is in the future.
    

    使用的格式 strptime() manual pages .