C#中的TimeSpan.FromDays()方法用于返回代表指定天数的TimeSpan,其中指定的精确度精确到最近的毫秒。
语法如下-
public static TimeSpan FromDays (double val);
上面的参数val是天数,精确到最接近的毫秒。
现在让我们看一个例子-
using System; public class Demo { public static void Main(){ TimeSpan span1 = new TimeSpan(5, 25, 0); TimeSpan span2 = new TimeSpan(1, 10, 0); TimeSpan span3 = TimeSpan.FromDays(43.999999); Console.WriteLine("TimeSpan1 = "+span1); Console.WriteLine("TimeSpan2 = "+span2); Console.WriteLine("TimeSpan3 = "+span3); Console.WriteLine("Result (Comparison of span1 and span2) = "+TimeSpan.Compare(span1, span2)); } }
输出结果
这将产生以下输出-
TimeSpan1 = 05:25:00 TimeSpan2 = 01:10:00 TimeSpan3 = 43.23:59:59.9140000 Result (Comparison of span1 and span2) = 1
现在让我们来看另一个示例-
using System; public class Demo { public static void Main(){ TimeSpan span1 = new TimeSpan(-3, 15, 0); TimeSpan span2 = new TimeSpan(-2, 05, 10); TimeSpan span3 = TimeSpan.FromDays(0.000323456); 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 = -02:45:00 TimeSpan2 = -01:54:50 TimeSpan3 = 00:00:27.9470000 Result (Comparison of span1 and span2) = -1 Result (Comparison of span2 and span3) = -1