Parse Simple DateTime

后端 未结 3 914
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 19:40
DateTime dt = DateTime.ParseExact(\"1122010\", \"Mddyyyy\", System.Globalization.CultureInfo.CurrentCulture);

Throwing this exception: String was

3条回答
  •  别那么骄傲
    2021-01-17 20:00

    I suggest using the format "MMddyyyy" and ensuring your input parameter has at least 8 characters. Example:

    DateTime dt = DateTime.ParseExact("1122010".PadLeft(8, '0'), "MMddyyyy", System.Globalization.CultureInfo.CurrentCulture);
    

    If you are using a data source with the leading 0 missing for the month, this will add it where required.

提交回复
热议问题