C# 中 TimeSpan.FromHours() 方法
C# 中的 TimeSpan.FromHours() 方法用于返回表示指定小时数的一个 TimeSpan,该指定精确到最近的毫秒。
语法
语法如下 −
public static TimeSpan FromHours (double val);
上面,值 val 是精确到最近毫秒的小时数。
示例
现在我们来看看一个示例 −
using System; public class Demo { public static void Main(){ TimeSpan span1 = TimeSpan.FromDays(0.000323456); TimeSpan span2 = new TimeSpan(-2, 05, 10); TimeSpan span3 = TimeSpan.FromHours(5); Console.WriteLine("TimeSpan1 = "+span1); Console.WriteLine("TimeSpan2 = "+span2); Console.WriteLine("TimeSpan3 = "+span3); Console.WriteLine("Result (Comparison of span1 and span2) = "+TimeSpan.Compare(span1, span2)); Console.WriteLine("Result (Comparison of span2 and span3) = "+TimeSpan.Compare(span2, span3)); } }
输出
这将产生以下输出 −
TimeSpan1 = 00:00:27.9470000 TimeSpan2 = -01:54:50 TimeSpan3 = 05:00:00 Result (Comparison of span1 and span2) = 1 Result (Comparison of span2 and span3) = -1
示例
现在我们来看看另一个示例 −
using System; public class Demo { public static void Main(){ TimeSpan span1 = TimeSpan.FromDays(15); TimeSpan span2 = new TimeSpan(5, 15, 30); TimeSpan span3 = TimeSpan.FromHours(5); Console.WriteLine("TimeSpan1 = "+span1); Console.WriteLine("TimeSpan2 = "+span2); Console.WriteLine("TimeSpan3 = "+span3); Console.WriteLine("Result (Comparison of span1 and span2) = "+TimeSpan.Compare(span1, span2)); Console.WriteLine("Result (Comparison of span2 and span3) = "+TimeSpan.Compare(span2, span3)); Console.WriteLine("Result (Comparison of span1 and span3) = "+TimeSpan.Compare(span1, span3)); } }
输出
这将产生以下输出 −
TimeSpan1 = 15.00:00:00 TimeSpan2 = 05:15:30 TimeSpan3 = 05:00:00 Result (Comparison of span1 and span2) = 1 Result (Comparison of span2 and span3) = 1 Result (Comparison of span1 and span3) = 1
广告