what is the format of 30-nov-2023 in python

To convert the date "30-nov-2023" to a specific format in Python, you can use the datetime module along with the strftime() method. Here's how you can do it:

main.py
from datetime import datetime

date_str = "30-nov-2023"

# convert the string to datetime object
date_obj = datetime.strptime(date_str, "%d-%b-%Y")

# format the datetime object into the desired format
formatted_date = date_obj.strftime("%Y-%m-%d")

print(formatted_date)
272 chars
12 lines

In this code, we first use the strptime() method to convert the given string to a datetime object. The format code %d represents the day, %b represents the abbreviated month name, and %Y represents the year with century as a decimal.

Then, we use the strftime() method to format the datetime object into the desired format. In this case, we are using %Y for the four-digit year, %m for the month, and %d for the day, separated by hyphens.

The output of the above code will be:

main.py
2023-11-30
11 chars
2 lines

Make sure to choose the appropriate format codes based on the format of the input date string you have.

related categories

gistlibby LogSnag