Pythonic difference between two dates in years?

后端 未结 12 2503
醉酒成梦
醉酒成梦 2020-12-02 16:21

Is there a more efficient way of doing this below? I want to have the difference in years between two dates as a single scalar. Any suggestions are welcome.

         


        
12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 17:11

    Just do this:

    from dateutil.relativedelta import relativedelta
    
    myBirthday = datetime.datetime(1983,5,20,0,0,0,0)
    now = datetime.datetime.now()
    
    
    
    difference = relativedelta(now, myBirthday)
    print("My years: "+str(difference.years))
    

提交回复
热议问题