Comparing two date strings in Python

前端 未结 6 998
庸人自扰
庸人自扰 2020-11-29 07:56

Let\'s say I have a string: \"10/12/13\" and \"10/15/13\", how can I convert them into date objects so that I can compare the dates? For example to see which date is before

6条回答
  •  萌比男神i
    2020-11-29 08:30

    Use datetime.datetime.strptime.

    from datetime import datetime
    
    a = datetime.strptime('10/12/13', '%m/%d/%y')
    b = datetime.strptime('10/15/13', '%m/%d/%y')
    
    print 'a' if a > b else 'b' if b > a else 'tie'
    

提交回复
热议问题