使用上索布文化(hsb),转换为字符串的DateTime对象使用格式"d.M.yyyyH.mm.ss'hodź.'".例如ToString("G")返回"31. 12. 20115.06.07hodź." 2011年12月31日上午05:06:07
问题是尝试将这样的字符串转换回DateTime不会导致true.甚至更简单的字符串如"1. 1. 2011"或"1.1.2011"也没有成功.并且万一有人建议在转换/坚持时传递文化:我当然这样做了.
尝试解析"1.2.3"会导致当前日期的时间为01:02:03.
我认为这是一个错误.或者有人知道可能出现什么问题吗?
我在Windows 8 RTM计算机上使用.NET 4.5 RTM.
样品:
DateTime date = DateTime.Now;
CultureInfo culture = new CultureInfo("hsb");
string dateString = date.ToString("G", culture);
DateTime convertedDate;
bool dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay);
// This results false although the date string was read by
// ToString("G") (i.e. '20. 9. 2012 12.28.10 hod?.') and should be okay
dateString = "1. 1. 2000";
dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay);
// This results in false although the date string should be okay
dateString = "1.1.2000";
dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay);
// This results also in false
dateString = "1.2.3";
dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay + ": " + convertedDate);
// This results strangely in true. The converted date is the current date
// with time 01:02:03.
Run Code Online (Sandbox Code Playgroud)
在 .Net 4.5 中,“hsb”被标记为中性区域性,因此所有 DateTime 解析将由标准格式提供程序完成。将 DateTime.ParseExact 与格式字符串一起使用。 http://www.codeproject.com/Articles/3612/Use-of- Different-Date-Time-formats-and-Culture-typ
===============================
我发现当 CultureInfo 中的标志“IsNeutralCulture”等于“true”时,日期字符串会以不变格式(en-US)进行解析。当我传递格式 MM/dd/yyyy DateTime.TryParse 时,可以正确解析文化“hsb”的日期。
以及我提供的文章中的一些引用:“只能为不变区域性或特定区域性创建 DateTimeFormatInfo,不能为中性区域性创建。DateTimeFormatInfo 继承自 Object 并实现 ICloneable 和 IFormarProvider 接口。”
我发现 DateTimeFormatInfo 是为“hsb”文化指定的,但正如我之前所说,IsNeutralCulture = true。我期望在.Net框架4.5中,当“IsNeutralCulture”等于“true”时,DateTimeFormatInfo不用于解析日期
| 归档时间: |
|
| 查看次数: |
654 次 |
| 最近记录: |