Convert.DateTime throws error: String was not recognized as a valid DateTime for “06-13-2012”

前端 未结 5 1941
小鲜肉
小鲜肉 2020-12-18 04:21

I am inserting a date into my database, the value which comes from:

s.theDate = Convert.ToDateTime(\"06-13-2012\");

and I get the error, \"

5条回答
  •  爱一瞬间的悲伤
    2020-12-18 04:49

    Just use ParseExact as already suggested or populate Convert.ToDateTime with the second parameter:

    Convert.ToDateTime("06-13-2012", new DateTimeFormatInfo{FullDateTimePattern = "MM-dd-yyyy"});
    

提交回复
热议问题