I need to parse dates and times from strings. The Problem is that the strings can have any possible format. But I also get the format strings.
So i get:
Date = "9/15/2010"
Time = "16:12:45"
DateFormat = "M/dd/yyyy"
TimeFormat = "h:mm:ss"
TimeZone = "+2:00:00" // +/- and time in TimeFormat
But i have some problems parsing these strings.
I can’t parse the time
DateTime.ParseExact("16:12:45","h:mm:ss",null,DateTimeStyles.None);
does not work and causes a FormatException. What is wrong with this call?
If the DateFormat contains slashes, i need to escape them @"M\/dd\/yyyy". Are there any other chars that would need escaping?
Can i parse the whole DateTime in one? Somehting like:
DateTime.ParseExact(Date+' '+Time+' '+TimeZone,DateFormat+' '+TimeFormat+' +'+TimeFormat,null,DateTimeStyles.None);
The “h:mm:ss” format string expects the hours element to be in 12-hour format (
h); The hours in your string are in 24-hour format so you need to useHinstead:Any literal character in your string that clashes with a format specifier will need to be escaped. For example,
/is the date separator but\/means the literal/character;:is the time separator but\:means the literal:character;yis one of the year specifiers but\yis the literalycharacter.Yes.