String was not recognized as a valid DateTime

前端 未结 3 826
野趣味
野趣味 2020-12-21 18:02

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.\"

st         


        
相关标签:
3条回答
  • 2020-12-21 18:37

    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"
    
    0 讨论(0)
  • 2020-12-21 18:52

    try this

    DateTime dt = DateTime.Parse(dtString,
    System.Threading.Tread.CurrentThread.CurrentCultur e.DateTimeFormat);
    
    0 讨论(0)
  • 2020-12-21 18:53

    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);
    
    0 讨论(0)
提交回复
热议问题