Convert YYYY-MM-DD to Aug 26, 2022

Question:

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

Asked By: jimmy

||

Answers:

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"
Answered By: JRose

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.

Answered By: kartik

here is your code:

from datetime import date

date_string = '2022-08-26'
date.fromisoformat(date_string).strftime('%b %d, %Y')  # 'Aug 26, 2022'
Answered By: SergFSM

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
Answered By: Vlad
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.