-1

I have date in the form of YYYY-MM-DD how can i format that date into Aug 26, 2022.

def return_date():
    date_returned = YYYY-MM-DD

return date_returned in form of Aug 26, 2022

jimmy
  • 45
  • 9

4 Answers4

2

here is your code:

from datetime import date

date_string = '2022-08-26'
date.fromisoformat(date_string).strftime('%b %d, %Y')  # 'Aug 26, 2022'
SergFSM
  • 1,419
  • 1
  • 4
  • 7
1

You can easily understand with this example:

from datetime import datetime
   # Get current Date
   date = datetime.now()
   # Represent dates in short textual format
   print("dd-MMM-yyyy:", date.strftime("%b %d, %Y"))

   # prints "dd-MMM-yyyy: Aug 26, 2022"

I hope this will help you.

kartik
  • 49
  • 7
1

Use strptime to decode the string then strftime to format it to your liking as follows:

from time import strptime, strftime


def return_date(ds): # data as string in the form YYYY-MM-DD
    return strftime('%b %d, %Y', strptime(ds, '%Y-%m-%d'))


print(return_date('2022-08-26'))

Output:

Aug 26, 2022
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

Is this what you are trying to accomplish?

import datetime

def return_date(ymd_format):
    year, month, day = ymd_format.split("-")
    return datetime.datetime(int(year), int(month), int(day)).strftime("%b %d, %Y")

print(return_date("2022-08-26")) # prints "Aug 26, 2022"
JRose
  • 1,382
  • 2
  • 5
  • 18
  • yes but the date argument is string with 2022-08-26 i tried with above logic but doesn't work your return is correct – jimmy Aug 26 '22 at 05:05
  • 2
    You should not split the string but rather use `strptime` `datetime.datetime.strptime("2022-08-26", "%Y-%m-%d").strftime("%b %d, %Y")` – Onyambu Aug 26 '22 at 05:39