TryParseExact返回假的,虽然我不知道为什么不知道为什么、TryParseExact

2023-09-03 04:46:48 作者:花街、卖笑的

方法 TryParseExact 在下面收益code座。 我想知道这是为什么。 我认为,这个日期2013年3月12日是无效的,因为这不是由斜线,但点分开。

在我改变了的CultureInfo 反德EN-US ,该方法返回。这可能是一个暗示,但我仍然不知道为什么会这样。

  VAR格式=新的String [] {YYYY / MM / DD};
VAR分析=新的日期时间();
VAR的结果= DateTime.TryParseExact(2013年3月12号,格式,
             新的CultureInfo(去-DE),DateTimeStyles.None,出解析);
 

解决方案

我觉得你现在的 DateSeparator (点)和 / 全自动本身代替它。

/ 分隔符有特殊的意义的与当前区域性的日期分隔符代替我的

  CultureInfo的C =新的CultureInfo(去DE);
Console.WriteLine(c.DateTimeFormat.DateSeparator); //打印。 (点)
 
我手机刚收了一条这信息,我不知道真的假的请高人帮我解决一下

看看的/ 自定义格式说明

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.

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);

解决方案

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

/ 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

相关推荐