将秒转换为(小时:分钟:秒:毫秒)时间的最佳方法是什么?
假设我有80秒,.NET中是否有任何专门的类/技术允许我将这些80秒转换为(00h:00m:00s:00ms)格式,比如DateTime或其他东西?
对于.Net <= 4.0使用TimeSpan类.
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);
(如Inder Kumar Rathore所述)对于.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");
(来自Nick Molyneux)确保秒小于TimeSpan.MaxValue.TotalSeconds
避免异常.
对于.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");
或者如果你想要日期时间格式,那么你也可以这样做
TimeSpan time = TimeSpan.FromSeconds(seconds); DateTime dateTime = DateTime.Today.Add(time); string displayTime = date.ToString("hh:mm:tt");
有关更多信息,您可以查看自定义TimeSpan格式字符串
如果您知道有几秒钟,则可以通过调用TimeSpan.FromSeconds来创建TimeSpan值:
TimeSpan ts = TimeSpan.FromSeconds(80);
然后,您可以获得天数,小时数,分钟数或秒数.或者使用其中一个ToString重载以您喜欢的任何方式输出它.
我做了一些基准测试,看看最快的方法是什么,这些是我的结果和结论.我运行了每个方法10M次并添加了评论,每次运行的平均时间.
如果您的输入毫秒不限于一天(结果可能是143:59:59.999),这些是从快到慢的选项:
// 0.86 ms static string Method1(int millisecs) { int hours = millisecs / 3600000; int mins = (millisecs % 3600000) / 60000; // Make sure you use the appropriate decimal separator return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hours, mins, millisecs % 60000 / 1000, millisecs % 1000); } // 0.89 ms static string Method2(int millisecs) { double s = millisecs % 60000 / 1000.0; millisecs /= 60000; int mins = millisecs % 60; int hours = millisecs / 60; return string.Format("{0:D2}:{1:D2}:{2:00.000}", hours, mins, s); } // 0.95 ms static string Method3(int millisecs) { TimeSpan t = TimeSpan.FromMilliseconds(millisecs); // Make sure you use the appropriate decimal separator return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", (int)t.TotalHours, t.Minutes, t.Seconds, t.Milliseconds); }
如果您的输入毫秒数限制为一天(结果将永远不会超过23:59:59.999),则这些选项可以从更快到更慢:
// 0.58 ms static string Method5(int millisecs) { // Fastest way to create a DateTime at midnight // Make sure you use the appropriate decimal separator return DateTime.FromBinary(599266080000000000).AddMilliseconds(millisecs).ToString("HH:mm:ss.fff"); } // 0.59 ms static string Method4(int millisecs) { // Make sure you use the appropriate decimal separator return TimeSpan.FromMilliseconds(millisecs).ToString(@"hh\:mm\:ss\.fff"); } // 0.93 ms static string Method6(int millisecs) { TimeSpan t = TimeSpan.FromMilliseconds(millisecs); // Make sure you use the appropriate decimal separator return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); }
如果你的输入只是几秒钟,那么方法会稍快一点.同样,如果您的输入秒数不限于一天(您的结果可能是143:59:59):
// 0.63 ms static string Method1(int secs) { int hours = secs / 3600; int mins = (secs % 3600) / 60; secs = secs % 60; return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, mins, secs); } // 0.64 ms static string Method2(int secs) { int s = secs % 60; secs /= 60; int mins = secs % 60; int hours = secs / 60; return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, mins, s); } // 0.70 ms static string Method3(int secs) { TimeSpan t = TimeSpan.FromSeconds(secs); return string.Format("{0:D2}:{1:D2}:{2:D2}", (int)t.TotalHours, t.Minutes, t.Seconds); }
如果您的输入秒数限制为一天(您的结果将永远不会超过23:59:59):
// 0.33 ms static string Method5(int secs) { // Fastest way to create a DateTime at midnight return DateTime.FromBinary(599266080000000000).AddSeconds(secs).ToString("HH:mm:ss"); } // 0.34 ms static string Method4(int secs) { return TimeSpan.FromSeconds(secs).ToString(@"hh\:mm\:ss"); } // 0.70 ms static string Method6(int secs) { TimeSpan t = TimeSpan.FromSeconds(secs); return string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds); }
作为最后的评论,让我补充一点,我注意到string.Format
如果你使用D2
而不是更快一点00
.
TimeSpan.FromSeconds(80);
http://msdn.microsoft.com/en-us/library/system.timespan.fromseconds.aspx
TimeSpan构造函数允许您在几秒钟内传递.只需声明一个TimeSpan类型的变量秒.例如:
TimeSpan span = new TimeSpan(0, 0, 500); span.ToString();