Add one year in current date PYTHON

后端 未结 8 1502
别跟我提以往
别跟我提以往 2020-11-29 01:59

I have fetched a date from database with the following variable

{{ i.operation_date }}

8条回答
  •  无人及你
    2020-11-29 02:23

    Here's one more answer that I've found to be pretty concise and doesn't use external packages:

    import datetime as dt
    # Assuming `day` has type dt.date
    one_year_delta = dt.timedelta(days=366 if ((day.month >= 3 and calendar.isleap(day.year+1)) or
                                               (day.month < 3 and calendar.isleap(day.year))) else 365)
    day += one_year_delta
    

提交回复
热议问题