In this guide, we will explore the TimeSpan.FromDays function in C# along with its syntax, arguments, and illustrations.
What is the TimeSpan.FromDays Method?
The C# TimeSpan structure deals with time spans expressed in days, hours, minutes, and seconds. It enables the calculation of time intervals. C# TimeSpan can be applied to DateTime instances to determine the gap between two given dates.
This method returns a TimeSpan, capable of representing a range of days down to the smallest increment of time.
Syntax:
It has the following syntax:
public static TimeSpan.FromDays (double value)
Parameter:
- Value: This variable is a number that is close to milliseconds.
- Return Value: It gives a new TimeSpan object that will own the value.
Exceptions:
An OverflowException occurs when a calculation produces a value smaller than the minimum allowed, larger than the maximum allowed, exceeding positive infinity, or falling below the minimum threshold, leading to an overflow situation.
ArgumentException: If the key doesn't exist.
TimeSpan Properties:
The TimeSpan class in C# includes properties like Days, Hours, Minutes, Seconds, Milliseconds, and Ticks. These properties are used to access the respective units of time stored in a TimeSpan object. Additionally, the TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds properties provide the total sum of each unit across the entire TimeSpan object.
Example 1:
Let's consider a C# code snippet to demonstrate the application of the TimeSpan.FromDays(Double) Method.
using System;
class TimeSp{
// Main Method
public static void Main()
{
try {
TimeSpan level = TimeSpan.FromDays(52.8799);
Console.WriteLine("The value of Timespan is : {0}", level);
}
catch (OverflowException ex)
{
Console.Write("The exception is thrown: ");
Console.Write("{0}", ex.GetType(), ex.Message);
}
}
}
Output:
The value of Timespan is : 52.21:07:03.3600000
Explanation:
- 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's explore a different C# program to demonstrate the utilization of the TimeSpan.FromDays(Double) method.
using System;
class TimeSp{
// Main Method
public static void Main()
{
try {
TimeSpan level = TimeSpan.FromDays(Double.NegativeInfinity);
Console.WriteLine("The value of Timespan is : {0}",level);
}
// the catch block for exception handling
catch (OverflowException ex)
{
Console.Write("The exception is thrown: ");
Console.Write("{0}", ex.GetType(), ex.Message);
}
}
}
Output:
The exception is thrown: System.OverflowException
Explanation:
In this instance, invoking the TimeSpan.FromDays(Double.NegativeInfinity) method will result in an OverflowException due to the supplied value falling outside the acceptable range for creating a TimeSpan instance. The exception handling mechanism within the catch block is responsible for managing this specific error condition and presenting the relevant error message. The provided code snippet serves as an illustration of utilizing the TimeSpan.FromDays(Double) method to generate a TimeSpan object representing a defined number of days. Should an invalid value be provided, it will trigger the OverflowException exception.
Conclusion:
In summary, the timeSpan.FromDays(Double) function in C# is employed to generate a TimeSpan instance based on a specified number of days. This method enables the measurement of time intervals and the computation of the time gap between two DateTime instances. The method expects a double type parameter representing the total number of days, including any fractional parts.
The TimeSpan class in C# presents a variety of properties that enable retrieval of the distinct elements within a TimeSpan instance, including Days, Hours, Minutes, Seconds, Milliseconds, and Ticks. Additionally, properties like TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds furnish the cumulative values of each TimeSpan component.
The code samples demonstrate the utilization of the TimeSpan.FromDays(Double) method to illustrate this approach. The initial instance showcases the instantiation of an object representing 52.8799 days, followed by its output on the console. In the subsequent demonstration, the TimeSpan object is instantiated with Double.NegativeInfinity as a parameter, resulting in an OverflowException being thrown.
In summary, the TimeSpan.FromDays(Double) method requires two arguments: one as a floating-point number and the other as a time span. This function proves valuable for managing time spans and computing variances within C# programs.