c# – “输入字符串格式不正确”尝试用’h’显示小时部分时出错

前端之家收集整理的这篇文章主要介绍了c# – “输入字符串格式不正确”尝试用’h’显示小时部分时出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用小时部分显示日期时间字段,而不使用单位数小时的前导零,如下所示:return string.Format(“{0:h}”,MyDateTimefield)但是我得到一个“输入字符串不在格式正确“错误.为什么?

return string.Format(“{0:hh}”,MyDateTimefield)有效.寻找正确的格式而不是解决方法.

解决方法

the pertinent docs开始:

If the “h” format specifier is used without other custom format specifiers,it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier,see 07001 later in this topic.

点击该链接后,您将进入:

To use any of the custom date and time format specifiers as the only specifier in a format string […],include a space before or after the specifier,or include a percent (“%”) format specifier before the single custom date and time specifier.

原文链接:https://www.f2er.com/csharp/98424.html

猜你在找的C#相关文章