什么是秒转换成最佳的方式(小时:分钟:秒:毫秒​​)的时间? [英] What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time?

查看:98
本文介绍了什么是秒转换成最佳的方式(小时:分钟:秒:毫秒​​)的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是秒转换成最佳的方式(小时:分钟:秒:毫秒​​)?时间

What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time?

让我们说我有80个秒时,是否有任何特殊的类/ .NET中的技术,让我对那些80秒进入转换(00H:00米:00秒:00ms)?格式喜欢的DateTime或东西。

Let's say I have 80 seconds, are there any specialized classes/techniques in .NET that would allow me to convert those 80 seconds into (00h:00m:00s:00ms) format like to DateTime or something?

推荐答案

对于净< = 4.0 :使用的时间跨度类

For .Net <= 4.0 Use the TimeSpan class.

TimeSpan t = TimeSpan.FromSeconds( secs );

string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", 
                t.Hours, 
                t.Minutes, 
                t.Seconds, 
                t.Milliseconds);

(正如因德尔·库马尔·拉索说)对于 .NET> 4.0 您可以使用

TimeSpan time = TimeSpan.FromSeconds(seconds);

//here backslash is must to tell that colon is
//not the part of format, it just a character that we want in output
string str = time .ToString(@"hh\:mm\:ss\:fff");

这篇关于什么是秒转换成最佳的方式(小时:分钟:秒:毫秒​​)的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆