[英]C# custom time format
Subtraction two time format , 减法两种时间格式,
string _time_One = "08:30" ;
string _time_Two = "08:35" ;
string _timeInterval = ( DateTime.Parse(_time_One) - DateTime.Parse(_time_Two) ).Minutes.ToString();
It give me the result 5
, but I want to show likes this format 00:05
. 它给我结果5
,但我想显示喜欢这种格式的00:05
。
Kindly show me how to format it . 请教我如何格式化它。 Thanks in advance ! 提前致谢 !
Subtracting two DateTime
gives you a TimeSpan
which has it's own formatting support using the ToString
method. 减去两个DateTime
可为您提供一个TimeSpan
,它使用ToString
方法具有自己的格式支持。
For example: 例如:
DateTime now = DateTime.Now;
DateTime later = now.AddMinutes(10);
TimeSpan span = later - now;
string result = span.ToString(@"hh\:mm");
Try this: 尝试这个:
string _time_One = "08:30";
string _time_Two = "08:35";
var span = (DateTime.Parse(_time_One) - DateTime.Parse(_time_Two));
string _timeInterval = string.Format("{0:hh\\:mm}", span);
For reference: Custom TimeSpan Format Strings . 供参考: 自定义TimeSpan格式字符串 。
@Lloyd is right here, but to clarify this for your case: @Lloyd就在这里,但请根据您的情况进行说明:
string _time_One = "08:30" ;
string _time_Two = "08:35" ;
TimeSpan ts = DateTime.Parse(_time_One) - DateTime.Parse(_time_Two);
MessageBox.Show(String.Format("Time: {0:00}:{1:00}", ts.Hours, ts.Minutes));
I hope this helps. 我希望这有帮助。
string _time_One = "08:30";
string _time_Two = "08:35";
string _timeInterval = (DateTime.Parse(_time_One) - DateTime.Parse(_time_Two)).Duration().ToString();
result=>00:05:00 结果=> 00:05:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.