2013-03-25 82 views

回答

5

不变文化是基于en-US,其中.不是日期分隔符。

您需要使用正确的文化,如fr-FR,它使用.作为日期分隔符。

您也可以使用DateTime.ParseExactDateTime.TryParseExact以及确切的格式字符串。

Convert.ToDateTime("31.03.2013", CultureInfo.GetCultureInfo("fr-FR")) 

或者

DateTime.ParseExact("31.03.2013", 
        "dd.MM.yyyy", 
        CultureInfo.InvariantCulture) 

会工作。

+2

...或带有格式说明符的“DateTime.ParseExact”。 – 2013-03-25 11:03:35

+0

......更不用说'en-US'使用'MM/dd/yyyy',而不是'dd/MM/yyyy' – Nolonar 2013-03-25 11:04:34

+0

小心:'mm'代表分钟,而不是几个月 – Nolonar 2013-03-25 11:10:50

0

如何

DateTime.ParseExact(date,"dd.MM.yyyy",null); 
0

你可以尝试使用DateTime.ParseExact方法。它应该为你做诡计。

DateTime.ParseExact("31.03.2013", "dd.MM.yyyy", CultureInfo.InvariantCulture); 
0

使用此代码 -

DateTime.ParseExact(date, "dd.MM.yyyy", CultureInfo.InvariantCulture); 
0

您可以DateTime.TryParseExact试试这个:

string date = "31.03.2013"; 
DateTime dateConverted; 
DateTime.TryParseExact(date, new string[] { "dd.MM.yyyy" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateConverted); 
Console.WriteLine(dateConverted);