C# 字符串未被识别为有效的日期时间

C# 字符串未被识别为有效的日期时间,c#,C#,我正在将uk日期格式字符串转换为US格式以将其保存到数据库中,但它会引发错误“字符串未被识别为有效的日期时间” 我也尝试过这个,但也是同样的例外 DateTime aa = DateTime.ParseExact(dateString, "MM/dd/yyyy", new System.Globalization.CultureInfo("en-GB")); 请让我知道如何将字符串中的英国日期格式转换为美国日期格式 谢谢。试试这个 DateTime dt = DateTime.Parse(dt

我正在将uk日期格式字符串转换为US格式以将其保存到数据库中,但它会引发错误“字符串未被识别为有效的日期时间”

我也尝试过这个,但也是同样的例外

DateTime aa = DateTime.ParseExact(dateString, "MM/dd/yyyy", new System.Globalization.CultureInfo("en-GB"));
请让我知道如何将字符串中的英国日期格式转换为美国日期格式

谢谢。

试试这个

DateTime dt = DateTime.Parse(dtString,
System.Threading.Tread.CurrentThread.CurrentCultur e.DateTimeFormat);

您指定了错误的格式。它应该是
dd/MM/yyyy

var dateString = "13/06/2011";
var aa = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.CurrentCulture);
使用en GB文化可以很好地工作:

string dateString = "13/06/2011";

DateTime aa = DateTime.Parse(dateString, new CultureInfo("en-GB"));
// aa.Day == 13
// aa.Month == 6
// aa.Year == 2011

string result = aa.ToString("d", new CultureInfo("en-US"));
// result == "6/13/2011"

因为第13个月不存在?当当前区域性不是en-UK时,使用当前线程的区域性无助于解析en-UK格式的值。你是对的,我的观点只是告诉他应该使用CurrentThread.CurrentCulture.DateTimeFormat,而不仅仅是CurrentThread.CurrentCulture,;)这对我来说是可行的,但在转换后,当我保存它并将其转换为UK时,它会将月份更改为天,将天更改为月。在var dateString=“13/06/2011”中;保存和检索后的日期为2011年6月13日,显示为2011年6月13日,但应显示为2011年6月13日。我已解决此问题
var date=dateValue.ToString(“dd/MM/yyyy”);这对我来说是可行的,但在转换后,当我保存它并将其转换为UK时,它会将月份更改为天,将天更改为月。在var dateString=“13/06/2011”中;保存和检索后的日期为2011年6月13日,显示为2011年6月13日,但应显示为2011年6月13日。我已解决此问题
var date=dateValue.ToString(“dd/MM/yyyy”);
string dateString = "13/06/2011";

DateTime aa = DateTime.Parse(dateString, new CultureInfo("en-GB"));
// aa.Day == 13
// aa.Month == 6
// aa.Year == 2011

string result = aa.ToString("d", new CultureInfo("en-US"));
// result == "6/13/2011"