mikepa88 mikepa88 - 8 months ago 58
C# Question

Error parsing single digit date

Code, simplified:

DateTime.ParseExact("12:53 PM - Aug 6, 2016", "hh:mm tt - MMM d, yyyy", CultureInfo.InvariantCulture);

Says it can't recognize string as a valid datetime value.
Only happens when day is a single digit. Source is third-party Web API, so changing the input string isn't a viable option (it should work like that)

Anyone sees something I don't?



You could try to remove whitespaces from the original string with Regex and change your parsing format as such:

DateTime.ParseExact(Regex.Replace("12:53 PM - Aug  6, 2016", "\\s", ""), "hh:mmtt-MMMd,yyyy", CultureInfo.InvariantCulture);

hope that helps