微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/98424.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐