Comparing two date strings in Python

前端 未结 6 1005
庸人自扰
庸人自扰 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条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 08:50

    import datetime
    
    d1="10/12/13"
    d2="10/15/13"
    date = d1.split('/')
    d1=datetime.datetime(int(date[2]),int(date[1]),int(date[0])) 
    date = d2.split('/')
    d2=datetime.datetime(int(date[2]),int(date[1]),int(date[0]))
    if d1 > d2 :
        ## Code
    today = datetime.datetime.today()
    if d1 > today :
        ## code
    

提交回复
热议问题