I am unable to convert strings to date with throwing exceptions of not proper format:
here are the patterens:
DD-MM-YYYY
MM-DD-YYYY
YYYY-DD-MM
YYYY-MM-DD
YYYYDDMM
YYYYMMDD
formats are coming from dropdown list and here is what I tried different method but still its throwing exceptions:
strCurrentFormat = rcboDateFormat.SelectedValue.ToString();
DateTime db = DateTime.ParseExact(strOldDate, "DD-MM-YYYY", CultureInfo.InvariantCulture);
//DateTime loadedDate = DateTime.ParseExact(strOldDate, strCurrentFormat, null);
I have solved this problem before by taking string, split them and move the year and month and day around to proper format but that would take long time, if anyone knows an easier way it would be a tremendous help.
Two things:
First – use the correct format strings.
DandYare not known format specifiers.dandyare.Second – you can put your formats into a string array and use the
ParseExactoverload that takes that list of formats.The formats will be attempted in order.