TimeSpan.FromDays() Method in C#


The TimeSpan.FromDays() method in C# is used to return a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.

Syntax

The syntax is as follows −

public static TimeSpan FromDays (double val);

Above, the parameter val is the number of days, accurate to the nearest millisecond.

Example

Let us now see an example −

 Live Demo

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));
   }
}

Output

This will produce the following output −

TimeSpan1 = 05:25:00
TimeSpan2 = 01:10:00
TimeSpan3 = 43.23:59:59.9140000
Result (Comparison of span1 and span2) = 1

Example

Let us now see another example −

 Live Demo

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));
   }
}

Output

This will produce the following output −

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

Updated on: 03-Dec-2019

521 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements