2

I want last friday of each month for upcoming three months.

Friday_date = datetime.date.today()

    while Friday_date.weekday() != 4:
        Friday_date += datetime.timedelta(1)

This gives me the nearest friday. I want to make sure this is the last friday of this month so that i can add 28 days to get next friday.

Madan Raj
  • 279
  • 4
  • 15

4 Answers4

4

The easiest way to do this is to use the module dateutil:

>>> from dateutil.relativedelta import FR, relativedelta
>>> datetime.date.today()+relativedelta(day=31, weekday=FR(-1))
datetime.date(2021, 6, 25)

Don't assume you can get the last Friday of subsequent months just by adding 28 days. It won't always work. Adding 28 days to the last Friday of February 2024 gives you this:

>>> datetime.date(2024,2,1)+relativedelta(day=31, weekday=FR(-1), days=28)
datetime.date(2024, 3, 22)

but the last Friday of that month is 29 March. Let dateutil do that correctly for you:

>>> datetime.date(2024,2,1)+relativedelta(day=31, weekday=FR(-1), months=1)
datetime.date(2024, 3, 29)
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • This is giving the last friday of the current month and i can add 28 easily to get upcoming months. – Madan Raj Jun 17 '21 at 07:31
  • 1
    Be careful of just adding 28 days. It won't always work. `datetime.date(2024,2,1)+relativedelta(day=31, weekday=FR(-1), weeks=4)` gives 22 March 2024 but the last Friday of that month is 29 March. But `datetime.date(2024,2,1)+relativedelta(day=31, weekday=FR(-1), months=1)` gives you the correct answer. – BoarGules Jun 17 '21 at 07:39
3

If needed with standard library only, here is with calendar and datetime:

import calendar
from datetime import date

today = date.today()
year, month = today.year, today.month

n_months = 4
friday = calendar.FRIDAY

for _ in range(n_months):
    # get last friday
    c = calendar.monthcalendar(year, month)
    day_number = c[-1][friday] or c[-2][friday]

    # display the found date
    print(date(year, month, day_number))

    # refine year and month
    if month < 12:
        month += 1
    else:
        month = 1
        year += 1

where the line c[-1][friday] or c[-2][friday] first checks the last week of the month: is Friday nonzero there? if so take it, else look at the week before where there must be a Friday.

This prints

2021-06-25
2021-07-30
2021-08-27
2021-09-24
Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
1

This formula gets you the day of the last Friday of any given month:

import calendar

year = 2021
month = 6

last_day = calendar.monthrange(year, month)[1]
last_weekday = calendar.weekday(year, month, last_day)
last_friday = last_day - ((7 - (4 - last_weekday)) % 7)
#                          ^    ^
#                          |    Friday
#                          days in a week

This is my first coffee, so this can probably be condensed a bit, but it illustrates the logic. last_day is the last calendar day (30 for June), last_weekday is what weekday it is (2 for Wednesday), and based on that we simply calculate how many days to subtract to land on the last Friday (25).

deceze
  • 510,633
  • 85
  • 743
  • 889
0

If you want to know the last friday you can do this :

from datetime import date
from datetime import timedelta

today = date.today()
offset = (today.weekday() - 4) % 7
last_wednesday = today - timedelta(days=offset)
Tomo
  • 71
  • 1
  • 7