[英]C# Does Not Recognize 'dd-MMM-yyyy' as Valid Datetime
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'.我在 C# 项目中有一个 function,它接受日期作为格式为“dd-MMM-yyyy”的字符串,因此示例为“10-MAR-2021”。
I need to convert that string into a datetime object, but everything I try results in我需要将该字符串转换为日期时间 object,但我尝试的一切都会导致
String '10-Mar-2021' was not recognized as a valid DateTime.
字符串“2021 年 3 月 10 日”未被识别为有效的日期时间。
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.
这在 .Net 4.6 中不是问题,但是自从升级到 .Net 5 后,日期格式突然变成了一个问题。
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.
当我复制粘贴异常中返回的字符串“10-Mar-2021”时,我得到了同样的错误。
Try尝试
char[] charArray = startdate.ToCharArray();
char[] charArray = startdate.ToCharArray();
and debug.和调试。 I get charArray[3] character with ASCII code 8206.
我得到 charArray[3] 字符,ASCII 码为 8206。
The code works for me when I don't copy-paste but write the startdate string myself.当我不复制粘贴而是自己编写 startdate 字符串时,该代码对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.