0

I need to use start date, end date, previous month first date, current month first data, previous month last date, previous month first data.start date and end date here are comming from parameters

This is my code i want to reduce it to very few lines.

        start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
        end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
        today = datetime.date.today()
        curr_mon_strt_date = datetime.datetime.strptime((str(datetime.date.today())[:-2] + "01"), '%Y-%m-%d')
        curr_mon_end_date = datetime.datetime.strptime(str(today), '%Y-%m-%d')
        prev_mon_strt_date = datetime.datetime.strptime(str((today.replace(day=1) - datetime.timedelta(days=1))[:-2] + "01"), '%Y-%m-%d')
        prev_mon_end_date = datetime.datetime.strptime((str(today.replace(day=1) - datetime.timedelta(days=1))), '%Y-%m-%d')

  • Not totally clear what you are asking for. You say you want 6 variables and your code has 7 lines. It won’t get fewer lines without getting more confusing. On the other hand, you don’t need to convert to/from string to do the calculations. Lastly, you’ll get the best help if you create a runnable example that others can copy and paste to run. – Glenn Gribble Dec 25 '20 at 05:16
  • See [this question](https://stackoverflow.com/questions/42950/how-to-get-the-last-day-of-the-month). – Glenn Gribble Dec 25 '20 at 05:26

0 Answers0