mikepa88 mikepa88 - 2 months ago 12
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?

Thanks

Answer

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