简体   繁体   English

字符串未被识别为有效的 DateTime

[英]String was not recognized as a valid DateTime

I am converting the uk date format string to US format to save this into database but it throw me error "String was not recognized as a valid DateTime."我正在将英国日期格式字符串转换为美国格式以将其保存到数据库中,但它向我抛出错误“字符串未被识别为有效的日期时间”。

string dateString = "13/06/2011";
DateTime dt = DateTime.Parse(dateString);

I have also tried this but same exception.我也试过这个但同样的例外。

DateTime aa = DateTime.ParseExact(dateString, "MM/dd/yyyy", new System.Globalization.CultureInfo("en-GB"));

Please let me know how can i convert uk format date in string to us date format.请让我知道如何将字符串中的 uk 格式日期转换为我们的日期格式。

Thanks.谢谢。

You have specified the wrong format.您指定了错误的格式。 It should be dd/MM/yyyy :它应该是dd/MM/yyyy

var dateString = "13/06/2011";
var aa = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.CurrentCulture);

DateTime.Parse with an en-GB culture works fine:具有 en-GB 文化的DateTime.Parse可以正常工作:

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"

try this尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM