C# Program to Calculate Period Duration

«
»

This is a C# Program to calculate period duration.

Problem Description

This C# Program Calculates Period Duration.

Problem Solution

Here the period duration is found.

advertisement
Program/Source Code

Here is source code of the C# Program to Calculate Period Duration. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Calculate Period Duration
 */
using System;
class CompareDates
{
 
    public static void Main()
    {
        DateTime today = DateTime.Now;
        DateTime yesterday = today - new TimeSpan(1, 0, 0, 0);
        DateTime tomorrow = today + new TimeSpan(1, 0, 0, 0);
        Console.WriteLine("Yesterday was     {0}", yesterday);
        Console.WriteLine("Today     is      {0}", today);
        Console.WriteLine("Tomorrow  will be {0}", tomorrow);
        Console.WriteLine("\nIs yesterday less than today?   {0}.",
           yesterday < today);
        Console.WriteLine("Is today the same as tomorrow ? {0}.",
           today == tomorrow);
 
        TimeSpan totalTimespan = new TimeSpan(3, 5, 24, 17) +
                                 new TimeSpan(1, 18, 35, 43);
        Console.WriteLine(
           "\nThe length of the period is {0} days {1} hours" +
           " {2} minutes {3} seconds.",
           totalTimespan.Days, totalTimespan.Hours,
           totalTimespan.Minutes, totalTimespan.Seconds);
        Console.ReadLine();
    }
 
}
Program Explanation

This C# program is used to calculate the period duration. The DateTime is a structure that is available from the System namespace within the base class libraries. This structure has a static method called ‘Now’ that returns the current time.

advertisement

There are a large number of additional data members and methods within the DateTime structure. Then TimeSpan(Int32, Int32, Int32) is used to Initialize a new instance of the TimeSpan structure to a specified number of hours, minutes, and seconds.

Compute the yesterday date by subtracting the value of today date by the value of user defined in Timespan(). Compute the tomorrow date by adding the value of today date with the value of user defined in Timespan().

The totalTimespan.Days is used to get the day’s component of the time interval represented by the current TimeSpan structure. The totalTimespan.Hours is used to get the hour’s component of the time interval represented by the current TimeSpan structure.

advertisement

The totalTimespan.Minutes is used to get the minute’s component of the time interval represented by the current TimeSpan structure. And the totalTimespan.Seconds is used to get the seconds component of the time interval represented by the current TimeSpan structure. Print the period duration.

Runtime Test Cases
Yesterday was     09-06-2014 15:52:34
Today     is      10-06-2014 15:52:34
Tomorrow  will be 11-06-2014 15:52:34
Is yesterday less than today?   True.
Is today the same as tomorrow ? False.
The length of the period is 5 days 0 hours 0 minutes 0 seconds.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn