将字符串转换为datetime问题

将字符串转换为datetime问题

问题描述:

我一直在网上寻找一个例子,将解决我的问题与字符串转换为日期时间。将字符串转换为datetime问题

我想将en-us(mm/dd/yyyy)转换为荷兰比利时(dd/mm/yyyy)日期时间。 你能给我举个例子吗?

为了您的信息,我已经尝试过Convert.ToDateTime,Parse,TryParse,ParseExact等,但都没有工作。我非常喜欢这个转换的例子,没有任何无用的链接。


Upate

我得到的错误是:字符串未被识别为有效的DateTime。 我曾尝试:

---------------- 
string str = "02/20/2012"; 
DateTime dt = Convert.ToDateTime(str); 
--------------------- 
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("nl-BE", true); 
DateTime theDateTime = DateTime.ParseExact(deliveryDate, "dd/mm/yyyy", theCultureInfo); 
Console.WriteLine(dt); 
---------------------- 
var parsed = DateTime.ParseExact("02/20/2012","dd/mm/yyyy", null); 
--------------- 

dateValue = DateTime.Parse(dateString, new CultureInfo("nl-BE", false)); 

和我不记得他们现在其他一些例子。但所有领先的没有。

+0

你想要字符串转换为字符串吗? – 2012-08-17 18:22:33

+0

DateTime应该支持CultureInfo。请参阅:http://stackoverflow.com/questions/313781/localization-of-date-time-using-custom-patterns – Brandon 2012-08-17 18:24:44

+2

我向你保证,ParseExact是workng罚款。 – 2012-08-17 18:25:22

使用这些方法重载:

,并通过一个CultureInfo.DateTimeFormat对他们说:

string enUsDateString = "12/31/2012"; 
IFormatProvider enUsDateFormat = new CultureInfo("en-US").DateTimeFormat; 

DateTime date = DateTime.Parse(enUsDateString, enUsDateFormat); 

IFormatProvider nlBeDateFormat = new CultureInfo("nl-BE").DateTimeFormat; 
string nlBeDateString = date.ToString(nlBeDateFormat); 

这种意志,然而,也包括时间分量在输出中。如果你不想那样,例如:

IFormatProvider nlBeDateFormat = new CultureInfo(…).DateTimeFormat.ShortDatePattern; 
//                ^^^^^^^^^^^^^^^^^