1

I have a code where the date is an input converted to a date:

InvDateStr = input("Enter the invoice date (YYYY-MM-DD): ")

InvDate = datetime.datetime.strptime(InvDateStr, "%Y-%m-%d")

I would like to later output this date in the format of "DD-Mon-YY"

For example: 2022-03-30 to 30-Mar-22

  • 1
    [`strftime`](https://docs.python.org/3/library/datetime.html#datetime.date.strftime) – Peter Wood Oct 18 '22 at 16:31
  • Does this answer your question? [How do I turn a python datetime into a string, with readable format date?](https://stackoverflow.com/questions/2158347/how-do-i-turn-a-python-datetime-into-a-string-with-readable-format-date) Please do some research before asking on SO (a quick Google search would give you the answer) – Pranav Hosangadi Oct 18 '22 at 16:32

2 Answers2

2

This will do what you require

InvDateStr = input("Enter the invoice date (YYYY-MM-DD): ")

InvDate = datetime.datetime.strptime(InvDateStr, "%Y-%m-%d")

InvDate = InvDate.strftime("%d-%b-%Y")
print(InvDate)

it uses strftime - more information on it can be found here https://pynative.com/python-datetime-format-strftime/#h-represent-dates-in-numerical-format

the %b simply translates a datetime month item into a 3 letter abbreviation. NOTE .strftime only works on datetime objects and not on str types

Hope this helps!

Max Davies
  • 148
  • 10
0

Maybe this is the code you are looking for:

year = input("Enter the year: ")
month = input("Enter the month: ")
day = input("Enter te day: ")
    
print(type(year))

if month == str(1):
    print(f"{day}-Jan-{year[2:]}")
elif month == str(2):
    print(f"{day}-Feb-{year[2:]}")
elif month == str(3):
    print(f"{day}-Mar-{year[2:]}")

and continuing the same way until we reach month == str(12) which is Dec

useeeeer132
  • 178
  • 1
  • 10