Home > Mobile >  how to add month abbr to string and create Dynamic string using python
how to add month abbr to string and create Dynamic string using python

Time:09-16

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

CodePudding user response:

  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')}")
  • Related