How to parse string dates with 2-digit year?

前端 未结 6 1983
天命终不由人
天命终不由人 2020-12-17 07:42

I need to parse strings representing 6-digit dates in the format yymmdd where yy ranges from 59 to 05 (1959 to 2005). According to the time module

6条回答
  •  自闭症患者
    2020-12-17 08:13

    Prepend the century to your date using your own pivot:

      year = int(date[0:2])
      if 59 <= year <= 99:
          date = '19' + date
      else
          date = '20' + date
    

    and then use strptime with the %Y directive instead of %y.

提交回复
热议问题