简体   繁体   中英

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.

Thanks.

You have specified the wrong format. It should be 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:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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