当解析奇怪的错误字符串到日期?字符串、奇怪、错误、日期

2023-09-04 00:43:36 作者:楠初

当我试图解析日期是这样的:

 日期时间T1 = DateTime.ParseExact(2013年8月11日,下午十一时00分00秒,MMMM DD,YYYY,HH:MM:SS TT,System.Globalization .CultureInfo.InvariantCulture);
 

它工作正常,但是当我做的事情是这样的:

 字符串s =2013年8月11号,下午十一时00分00秒;
日期时间T = DateTime.ParseExact(S,MMMM DD,YYYY,HH:MM:SS TT,System.Globalization.CultureInfo.InvariantCulture);
 
字符串和日期时间的处理

我得到这个错误:

 键入System.FormatException的异常mscorlib.ni.dll发生,但在用户code没有处理
 

解决方案

由于您的字符串

 字符串s =2013年8月11号,下午十一时00分00秒;
 

含 0x200e(8206)字符的开始和结束的对。您可以通过

很容易看出来

  VAR字符= s.ToCharArray();
 

似乎是一个复制粘贴+问题

您可以删除这些字符:的

  VAR中newstr =新的字符串(s.Where(C => C< 128).ToArray())
 

When I try to parse date like this:

DateTime t1 = DateTime.ParseExact("August 11, 2013, 11:00:00 PM", "MMMM dd, yyyy, hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

It works correctly but when I do thing like this :

string s ="‎August ‎11, ‎2013, ‏‎11:00:00 PM";
DateTime t = DateTime.ParseExact(s, "MMMM dd, yyyy, hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

I get this error :

An exception of type 'System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code

解决方案

Because your string

string s = "‎August ‎11, ‎2013, ‏‎11:00:00 PM";

Includes 0x200e(8206) character at the beginning and end of August. You can see it easily by

var chars = s.ToCharArray();

Seems to be a copy+paste problem

You can remove those chars by:

var newstr = new string(s.Where(c => c <128).ToArray())