加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:24:18 所属栏目:百科 来源:网络整理
导读:我正在尝试使用小时部分显示日期时间字段,而不使用单位数小时的前导零,如下所示:return string.Format(“{0:h}”,MyDateTimefield)但是我得到一个“输入字符串不在格式正确“错误.为什么? return string.Format(“{0:hh}”,MyDateTimefield)有效.寻找正
我正在尝试使用小时部分显示日期时间字段,而不使用单位数小时的前导零,如下所示: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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读