TryParseExact返回假的,虽然我不知道为什么 [英] TryParseExact returns false, though I don't know why

查看:157
本文介绍了TryParseExact返回假的,虽然我不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

我觉得你现在的<一个href="http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.dateseparator.aspx"相对=nofollow> 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

这篇关于TryParseExact返回假的,虽然我不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆