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

c# – 如何将TimeSpan转换为24小时和分钟字符串?

我使用这个代码将Timespan转换为String(例如:14:53):
myTimeSpan.ToString("hh:mm");

但是发生这个错误

Input string was not in a correct format

这样做的正确方法是什么?

解决方法

myTimeSpan.ToString(@"hh\:mm")

Custom TimeSpan Format Strings

The custom TimeSpan format specifiers do not include placeholder separator symbols,such as the symbols that separate days from hours,hours from minutes,or seconds from fractional seconds. Instead,these symbols must be included in the custom format string as string literals. For example,“dd.hh\:mm” defines a period (.) as the separator between days and hours,and a colon (:) as the separator between hours and minutes.

原文地址:https://www.jb51.cc/csharp/96199.html

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

相关推荐