how to add month abbr to string and create Dynamic string using python

Question:

I want make string Dynamic with Month abbr in it.

for eg.

01] In current Month (Means Sep) it will print below string.

i.e: payment links that are yet to be renewed for the month of Aug to Oct 2022

02] In Next Month (Means Oct) it will print below string.

i.e: payment links that are yet to be renewed for the month of Sep to Nov 2022

Asked By: beginner_Coder

||

Answers:

  1. You ignore the corner cases. For instance, if current month is Dec. 2022, what you expect should be Nov. 2022 to Jan. 2023, not Nov. to Jan. 2022.

  2. You can use dateutil

import datetime
from dateutil import relativedelta

now = datetime.date.today()
prev_month = now - relativedelta.relativedelta(months=1)
next_month = now + relativedelta.relativedelta(months=1)
print(f"payment links that are yet to be renewed for the month of {prev_month.strftime('%b %Y')} to {next_month.strftime('%b %Y')}")
Answered By: Will Zhao
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.