Add one year in current date PYTHON

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

I have fetched a date from database with the following variable

{{ i.operation_date }}

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 02:31

    Another way would be to use pandas "DateOffset" class

    link:-https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.tseries.offsets.DateOffset.html

    Using ASGM's code(above in the answers):

    from datetime import datetime
    import pandas as pd
    
    your_date_string = "April 1, 2012"
    format_string = "%B %d, %Y"
    
    datetime_object = datetime.strptime(your_date_string, format_string).date()
    new_date = datetime_object + pd.DateOffset(years=1)
    
    new_date.date()
    

    It will return the datetime object with the added year.

    Something like this:-

    datetime.date(2013, 4, 1)
    

提交回复
热议问题