calculate data for upcoming year in Python

I am very new to python. May someone please please help me what the below code output will be

Code 1.

change_qtr_last_day = {datetime.datetime(2018, 9, 30).date(): datetime.datetime(2018, 9, 29).date(),
                datetime.datetime(2019, 9, 30).date(): datetime.datetime(2019, 9, 28).date(),
                datetime.datetime(2019, 3, 31).date(): datetime.datetime(2019, 3, 30).date()}

code2:

change_qtr_last_day = {datetime.datetime(2019, 9, 30).date(): datetime.datetime(2019, 9, 29).date(),
                datetime.datetime(2020, 9, 30).date(): datetime.datetime(2020, 9, 28).date(),
                datetime.datetime(2020, 3, 31).date(): datetime.datetime(2021, 3, 30).date()}

Many Thanks

Answer

**The output of the first script will be: **

{datetime.date(2018, 9, 30): datetime.date(2018, 9, 29), datetime.date(2019, 9, 30): datetime.date(2019, 9, 28), datetime.date(2019, 3, 31): datetime.date(2019, 3, 30)}

**Output of the second script will be: **

{datetime.date(2019, 9, 30): datetime.date(2019, 9, 29), datetime.date(2020, 9, 30): datetime.date(2020, 9, 28), datetime.date(2020, 3, 31): datetime.date(2021, 3, 30)}

If you are wondering why it isn’t only showing the date and is showing the function name, it is because datetime.datetime().date() returns a class

But if you want to only see the it as a proper date instead of class, use the str() function Eg:

change_qtr_last_day = {str(datetime.datetime(2018, 9, 30).date()): str(datetime.datetime(2018, 9, 29).date())}

output:

{'2018-09-30': '2018-09-29'}

Leave a Reply

Your email address will not be published. Required fields are marked *