我想输入的字符串格式化为 MM / DD / YYYY HH:MM:SS C#中的格式 输入的字符串格式 MM / DD / YYYY HH:MM:SS 例如:2013年4月30日23:00
我试过 Convert.ToDateTime()的功能,但它认为4日期和3月为这不是我想要的。其实月份是04和日期03。
我试过 DateTime.ParseExact()函数也,但是,得到异常
我收到错误:
字符串未被识别为有效日期时间。
解决方案您的日期时间字符串不包含任何秒。您需要反映在您的格式(删除:SS )。结果此外,您还需要指定 ^ h 而不是 ^ h 如果您正在使用24小时次:
DateTime.ParseExact(2013年4月30日23时,MM / DD / YYYY HH:MM,CultureInfo.InvariantCulture)在这里看到更多的信息:
的自定义日期和时间格式字符串
I want to format the input string into MM/dd/yyyy hh:mm:ss format in C#. The input string is in format MM/dd/yyyy hh:mm:ss For example :"04/30/2013 23:00"
I tried Convert.ToDateTime() function, but it considers 4 as date and 3 as month which is not what I want. Actually month is 04 and date is 03.
I tried DateTime.ParseExact() function also, But getting Exception.
I am getting error:
String was not recognized as a valid DateTime.
解决方案
Your date time string doesn't contains any seconds. You need to reflect that in your format (remove the :ss). Also, you need to specify H instead of h if you are using 24 hour times:
DateTime.ParseExact("04/30/2013 23:00", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)See here for more information:
Custom Date and Time Format Strings
更多推荐
DateTime格式问题:字符串未被识别为有效的DateTime
发布评论