This question already has answers here:
Why can't DateTime.ParseExact parse DateTime output?
                                
                                    (4个答案)
                                
                        
                                2年前关闭。
            
                    
The documentation on yyy format表示是3位或更多。那么,为什么以下代码会引发异常?

string format = "dd.MM.yyy";
string s = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
DateTime d = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture); //this throws FormatException


Fiddle

最佳答案

在我看来,这似乎是个错误。我试图以此排除任何语言环境问题,并且也遇到了相同的错误:

 string format = "dd.MM.yyy";
 string s = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
 DateTime d = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture); //this throws FormatException


编辑:看起来是这个问题的重复:Why can't DateTime.ParseExact parse DateTime output?

关于c# - 为什么DateTime.ParseExact会引发“dd.Mm.yyyy”格式的异常和带有4位数字的年份的日期? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46000493/

10-11 19:38
查看更多