How to format date string via multiple formats in python
This actually a problem i was facing and this how i approached it, my main purpose was to the date seperators
class InputRequest: "This class contain all inputs function that will be required in this program. " def __init__(self, stockTickerName = 'YHOO', stockSite='yahoo', startDate = None, endDate = datetime.date.today()): def requestInput(self, requestType =''): "Fro requesting input from user" self.__requestInput = input(requestType) return self.__requestInputdef dateFormat(self, dateType=''): ''' this function handles user date input this repeats until the correct format is supplied dataType: this is the type of date, eg: DOF, Date of Arriveal, etc ''' while True: try: dateString = InputRequest.requestInput(self,dateType) dtFormat = ('%Y/%m/%d','%Y-%m-%d','%Y.%m.%d','%Y,%m,%d','%Y\%m\%d') #you can add extra formats needed for i in dtFormat: try: return datetime.datetime.strptime(dateString, i).strftime(i) except ValueError: pass except ValueError: pass print('\nNo valid date format found. Try again:') print("Date must be seperated by either [/ - , . \] (eg: 2012/12/31 --> ): ")