How can I subtract or add 100 years to a datetime field in the database in Django?

后端 未结 5 1073
谎友^
谎友^ 2020-12-25 11:10

How can I subtract or add 100 years to a datetime field in the database in Django?

The date is in database, I just want to directly update the field wit

5条回答
  •  [愿得一人]
    2020-12-25 11:26

    I would use the relativedelta function of the dateutil.relativedelta package, which will give you are more accurate 'n-years ago' calculation:

    from dateutil.relativedelta import relativedelta
    import datetime
    
    years_ago = datetime.datetime.now() - relativedelta(years=5)
    

    Then simply update the date field as others have shown here.

提交回复
热议问题