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.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...