So if several date strings with different formats and am looking for a way to normalize dates given the format.
For example, if I have three sets of date strings and their associated format strings:
date = 11/20/2020
, format = MM/DD/YYYY
date = 11-20-2020
, format = MM-DD-YYYY
date = 20-11-2020
, format = DD-MM-YYYY
I am looking to normalize all of these dates into the format of YYYY-MM
. Is there a way I can normalize the date
if I am giving the format
? Can the datetime
package help here?
Nice to have: If there is a way to take in other types of dates too (ex: UTC format), that would be helpful as well
What I have so far is this:
date_list = re.split(r'\W+', date)
date_format_list = re.split(r'\W+', format)
year_index = date_format_list.index('YYYY')
month_index = date_format_list.index('MM')
formatted_year = date_list[year_index]
formatted_month = date_list[month_index]
'-'.join([formatted_year, formatted_month])
Update I am not able to change the format
string which is causing the issue