将字符串转换为datetime问题

Imi*_*xha -1 c#

我一直在网上寻找一个例子来解决我的问题,将字符串转换为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));
Run Code Online (Sandbox Code Playgroud)

和其他一些我现在不记得的例子.但所有领导都没有.

sta*_*ica 8

使用这些方法重载:

并传递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);
Run Code Online (Sandbox Code Playgroud)

但是,这也将包括输出中的时间组件.如果您不想这样,请尝试例如:

IFormatProvider nlBeDateFormat = new CultureInfo(…).DateTimeFormat.ShortDatePattern;
//                                                                ^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)