Format Date String in Python: 5th May 2023 Example
You can format the date in Python using the 'strftime()' method. Here's an example:
import datetime
date_str = '2023-05-05T19:47:05.192394+05:30'
date_obj = datetime.datetime.fromisoformat(date_str)
formatted_date = date_obj.strftime('%d %B %Y')
print(formatted_date)
Output:
05 May 2023
Explanation:
- The 'fromisoformat()' method is used to convert the date string to a datetime object.
- The 'strftime()' method is used to format the datetime object into the desired format, which is '%d %B %Y' in this case. '%d' represents the day of the month, '%B' represents the full month name, and '%Y' represents the year in four digits.
原文地址: https://www.cveoy.top/t/topic/nUrP 著作权归作者所有。请勿转载和采集!