0

Given today's date, what is the efficient way to retrieve the first and last date for previous 3 months (i.e. 3/1/2020' and '3/31/2020'; '2/1/2020' and '2/29/2020'; '1/1/2020' and '1/31/2020')?

EDIT

For previous month's first and last, the following code is working as expected. But I am not sure how to retrieve the previous 2nd and 3rd month's first and last date.

from datetime import date, timedelta

last_day_of_prev_month = date.today().replace(day=1) - timedelta(days=1)

start_day_of_prev_month = (date.today().replace(day=1) 
                            - timedelta(days=last_day_of_prev_month.day))

# For printing results
print("First day of prev month:", start_day_of_prev_month)
print("Last day of prev month:", last_day_of_prev_month)
Community
  • 1
  • 1
user182944
  • 7,897
  • 33
  • 108
  • 174
  • 3
    What have you tried ? – azro Apr 07 '20 at 17:26
  • Added a sample code I tried, it is giving me first and last date of previous month. But I am unsure how to retrieve the first and last date of 2nd and 3rd previous months first and last date. Please help – user182944 Apr 07 '20 at 17:34
  • I've update my answer, with the month indexes saved, this is easier – azro Apr 07 '20 at 18:01

2 Answers2

2

You may

  • get the 3 previous month
  • create the date with day 1, and last day by going to the next and remove 1 day
def before_month(month):
    v = [9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    return v[month:month + 3]

dd = datetime(2020, 4, 7)

dates = [[dd.replace(month=month, day=1), dd.replace(month=month, day=monthrange(dd.year, month)[1])]
         for month in before_month(dd.month)]

print(dates)

# [[datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 31, 0, 0)], 
#  [datetime.datetime(2020, 2, 1, 0, 0), datetime.datetime(2020, 2, 29, 0, 0)], 
#  [datetime.datetime(2020, 3, 1, 0, 0), datetime.datetime(2020, 3, 31, 0, 0)]]

I did not found another nice way to get the 3 previous month, but sometimes the easiest way it the one to use

azro
  • 53,056
  • 7
  • 34
  • 70
  • How to convert the datetime output into a String? – user182944 Apr 07 '20 at 18:13
  • 1
    @user182944 That is something you may look by yourself, just google it https://stackoverflow.com/questions/311627/how-to-print-a-date-in-a-regular-format – azro Apr 07 '20 at 18:30
1

You can loop over the 3 previous month, just update the date to the first day of the actual month at the end of every iteration:

from datetime import date, timedelta

d = date.today()

date_array = []
date_string_array = []

for month in range(1, 4):
    first_day_of_month = d.replace(day=1)
    last_day_of_previous_month = first_day_of_month - timedelta(days=1)
    first_day_of_previous_month = last_day_of_previous_month.replace(day=1)

    date_array.append((first_day_of_previous_month, last_day_of_previous_month))
    date_string_array.append((first_day_of_previos_month.strftime("%m/%d/%Y"), last_day_of_previos_month.strftime("%m/%d/%Y")))
    d = first_day_of_previos_month

print(date_array)
print(date_string_array)

Results:

[(datetime.date(2020, 3, 1), datetime.date(2020, 3, 31)), (datetime.date(2020, 2, 1), datetime.date(2020, 2, 29)), (datetime.date(2020, 2, 1), datetime.date(2020, 2, 29))]
[('03/01/2020', '03/31/2020'), ('03/01/2020', '03/31/2020'), ('03/01/2020', '03/31/2020')]