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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/csharp/96199.html

猜你在找的C#相关文章