I am writing a C # class that converts strings to dates. Pretty easy, I think. The class accepts a string format as "yyyy-MM-dd" and inputstrings as "2010-10-10"
However, I have some cases that give me problems:
format "yyyyMMdd" input "19950000"
or
format "dd-MM-yyyy" input "00-06-2001"
Note that these cases have zeros ('00') for the day and / or month and that they cannot be converted to DateTime. I need to replace them.
To deal with these cases, I need to split the input string in parts, one per day, month and year, so I can set some days and month by default (maybe 01) if they are missing. But for this I need to use formatstring.
So the question is, how can I split the input string in the components specified in stringstring format?
thank
[UPDATE] , :
string[] formats = { format, format.Replace("dd", "00").Replace("MM", "00"), format.Replace("dd", "00"), format.Replace("MM", "00") };
DateTime d = DateTime.ParseExact(txtDate.Text, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
('00') , .
!