TryParseExact返回false,虽然我不知道为什么

TryParseExact返回false,虽然我不知道为什么

问题描述:

方法 TryParseExact 在下面的代码块中返回 true

我想知道为什么。

我认为这个日期2013.03.12是无效的,因为这不是由斜线分隔,而是点。

Method TryParseExact in code block below returns true.
I would like to know why.
I think this date "2013.03.12" is invalid because this is not separated by slash but dot.

将$ code> CultureInfo de-De更改为en-US,该方法返回 false 。这可能是一个提示,但我仍然不知道为什么会发生这种情况。

After I changed the CultureInfo "de-De" to "en-US", the method returns false. This could be a hint but I still don't know why this happens.

var format = new string[] { "yyyy/MM/dd" };
var parsed = new DateTime();
var result = DateTime.TryParseExact("2013.03.12", format, 
             new CultureInfo("de-DE"), DateTimeStyles.None, out parsed);


我认为你目前的 DateSeparator (点)和 / 自动替换为

I think your current DateSeparator is . (dot) and / automaticly replace itself to it.

/ seperator具有替换当前文化日期分隔符

/ seperator has a special meaning of "replace me with the current culture's date separator"

CultureInfo c = new CultureInfo("de-DE");
Console.WriteLine(c.DateTimeFormat.DateSeparator); //Prints . (dot)

查看 /自定义格式说明符

Take a look at The "/" Custom Format Specifier