Changing input string date formats in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Changing input string date formats in Python

I am trying to figure out how to change user date string input formated as either "mm/dd/yyyy" or "month day, year" to a single format string like "yyyy,mm,dd" in Python. I have looked up tutorials on using datetime modules and pandas modules and they have me turning the string date into a datetime object I am unable to index through to extrapulate the numbers for a program I am working on to calculate the day of the week from a given date. Okay, using these modules, I am able to isolate the year, month, and date from the datetime object but I am unable to use those numbers to then call up a key value from a dictionary I created. Is there a simple way to convert a date string from one format to another date string format of my choosing?

17th Sep 2021, 6:04 AM
Robert Haugland
Robert Haugland - avatar
2 Answers
+ 1
Ipang, I am sure there is a module for figuring out the day of the week but I am trying to create a program on my own using a mathematical formula utilizong the Gregorian Calendar. The dictionary I created is linking month codes and century codes to plug into the formula, i.e. {"01"(Jaunuary): 0, "02": 3,.....}.
17th Sep 2021, 7:09 AM
Robert Haugland
Robert Haugland - avatar
0
Jay Matthews, I ran the program from the link and it seems to work only if I want today's date from Datetime. When d="11/19/2019"(date string of my choice) I get an error message. I found out how to chnage the output date from datetime to what ever date I choose and how to call just the year, the month or day, but when I try to use them as variables to call a month in my dictionary such as "01"(for January), I get an error saying that that a string is needed.
17th Sep 2021, 7:18 AM
Robert Haugland
Robert Haugland - avatar