Javatpoint Logo
Javatpoint Logo

TimeSpan.FromDays() method in C#

In this article, we will discuss TimeSpan.FromDays() method in C# with its syntax, parameters, and examples.

What is the TimeSpan.FromDays() Method?

The C# TimeSpan struct operates with time intervals represented in days, hours, minutes, and seconds. It allows for measuring two-time differences. C# TimeSpan is available to DateTime objects to discover the difference between two specified dates.

This approach gives a TimeSpan, which can represent any number of days up to the last millisecond.


It has the following syntax:


  • Value: This variable is a number that is close to milliseconds.
  • Return Value: It gives a new TimeSpan object that will own the value.


OverflowException: Any operation resulting in a number that is smaller than the smallest possible value, bigger than the largest possible value, greater than plus infinite, or smaller than misusing is considered to be an overflow.

ArgumentException: If the key doesn't exist.

TimeSpan Properties:

TimeSpan class of C# contains Days, Hours, Minutes, Seconds, Milliseconds, and Ticks as properties, which are assigned to return days, hours, minutes, seconds, and milliseconds in the TimeSpan object. The properties TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds return the totals for those on an object.

Example 1:

Let us take a C# Program to implement the TimeSpan.FromDays(Double) Method.


The value of Timespan is : 52.21:07:03.3600000


  • In this example, the FromDays(Double) method is called, and the operator's value is set to 52.8799. It ensures a new TimeSpan object indicating a particular number of days specified. The quotient represents the remaining fraction, thus a fraction of a day.
  • The TimeSpan object resulting from this would be saved in the level variable.
  • After that, the writeLine command is the one that prints out the level variable value on the console. The format string {0} is a place-holding which will be replaced with the value of level.
  • The execution of the try block will be interrupted by an OverflowException occurrence; the catch block will be executed. The OverflowException class presents an exception that throws when a numerical, casting, or conflictive conversion leads to an overflow.
  • The catch block shows the message on the console using Write method. The placeholder "{0}" in the format string is swapped with the type of the exception, including (ex.GetType()) and with the exception message (ex.Message).

Example 2:

Let us take another C# Program to implement the TimeSpan.FromDays(Double) Method.


The exception is thrown: System.OverflowException


In this example, the TimeSpan.FromDays(Double.NegativeInfinity) method call will throw an OverflowException because the value provided is not within the valid range that can be used to instantiate a TimeSpan object to fill this gap. The catch block will handle this error and display the correct error message. The given program showcases a sample way of using the method TimeSpan.FromDays(Double) to create a TimeSpan object depicting a specific number of days. In case of an incorrect value given, the exception OverflowException will be raised.


In conclusion, the timeSpan.FromDays(Double) method used in C# for creating a TimeSpan object for the given number of days. It allows us to measure intervals of time and calculate the difference between two DateTime objects. The function takes a double value parameter, which is the number of days, including the fractions.

The TimeSpan class of C# offers numerous properties that provide access to the individual components of a TimeSpan type, such as Days, Hours, Minutes, Seconds, Milliseconds, or Ticks. Moreover, properties like TotalDays, TotalHours, TotalMinutes, TotalSeconds and TotalMilliseconds provide the total value of each element of the TimeSpan object.

The code examples used the TimeSpan.FromDays(Double) method to display the usage of this technique. The first example is the result of creating the object representing 52.8799 days, and its printing is the value on the console. The second example aims to define the TimeSpan object by using Double.NegativeInfinity as an argument. This action causes throwing an OverflowException.

In conclusion, the TimeSpan.FromDays(Double) has two input parameters: one represents a decimal value, and the other represents a time span. It's a useful tool for working with time intervals and calculating differences in C# applications.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA