我正在尝试使用小时部分显示日期时间字段,而不使用单位数小时的前导零,如下所示: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.