7

Using the Upper Sorbian culture (hsb) a DateTime object converted to a string uses the format "d. M. yyyy H.mm.ss 'hodź.'". ToString("G") for example returns "31. 12. 2011 5.06.07 hodź." for the 31. of December 2011, 05:06:07 AM.

Problem is though that trying to convert such a string back to a DateTime does not result true. Even simpler strings like "1. 1. 2011" or "1.1.2011" lead to no success. And just in case someone suggests to pass the culture when converting/persing: I did that of course.

Trying to parse "1.2.3" results in the current date with the time 01:02:03.

I consider that a bug. Or does someone know what could be wrong?

I am using .NET 4.5 RTM on a Windows 8 RTM machine.

Sample:

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.
4

1 回答 1

0

在 .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。当“IsNeutralCulture”等于“true”时,我希望.Net framework 4.5 DateTimeFormatInfo 不用于解析日期

于 2013-04-02T13:39:58.660 回答