S
S
StenMarsh13372021-02-05 14:00:43
Python
StenMarsh1337, 2021-02-05 14:00:43

Do not display the day of the week if it is in the next month?

Hello everyone, tell me how to make a check so that it doesn’t display, for example
1. What exactly does the code do now: days of the week, except for those that are already behind today
2. What you need: to not display the days of the week, those that are already in the next month

bot.send_message(chat_id=message.chat.id,
                                  text=f"На який день?",
                                  reply_markup=day(user_id))

def day(user_id):
    markup = types.InlineKeyboardMarkup(row_width=1)
    d = datetime.now()
    wd = date.weekday(d)
    print(wd)
    days = ["Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье"]
    print(days[wd])
    my_days = days[d.weekday() + 1:]
    for i in my_days:
        markup.add(
            types.InlineKeyboardButton(text=i, callback_data=f"2")
        )
    return markup

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wispik, 2021-02-05
@StenMarsh1337

The first thing that comes to mind is: we count the number of days until the end of the month and truncate the my_days list if its length is greater than the number of days.

import calendar

...
days_month = calendar.monthrange(d.year, d.month)[1]
if len(my_days) > (days_month - d.day):
    my_days = my_days[:days_month - d.day]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question