I have a function in a C# project that accepts a date as a string in the format 'dd-MMM-yyyy', so an example would be '10-MAR-2021'.
I need to convert that string into a datetime object, but everything I try results in
String '10-Mar-2021' was not recognized as a valid DateTime.
I have tried
DateTime start = DateTime.ParseExact(startdate, "dd-MMM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime start = Convert.ToDateTime(startdate);
DateTime start = DateTime.Parse(startdate);
And they all throw the same error. This was not a problem in.Net 4.6, however since upgrading to.Net 5 that date format is a problem all of a sudden.
Any ideas?
I think you have some strange character inserted after the first dash. When I copy-paste the string "10-Mar-2021" returned in your exception, I get the same error.
Try
char[] charArray = startdate.ToCharArray();
and debug. I get charArray[3] character with ASCII code 8206.
The code works for me when I don't copy-paste but write the startdate string myself.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.