The string was not recognized as a valid DateTime. There is an unknown word starting at index 0

前端 未结 5 970
予麋鹿
予麋鹿 2021-01-19 10:53

I have the following C# that is giving me the error above when trying to parse string to datetime.

DateTime backupdate = System.Convert.ToDateTime(imageflowl         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-19 11:19

    ParseExact should work for you, assuming your users have no way of editing that value on their own; in that case, you should use TryParseExact to unless you want the FormatException.

    var toParse = "2012-04-15 15:23:34:123";
    
    var parsed = DateTime.ParseExact(toParse, "yyyy-MM-dd HH:mm:ss:fff", null);
    
    Assert.AreEqual(new DateTime(2012, 4, 15, 15, 23, 34, 123), parsed);
    

提交回复
热议问题