C# program to find the sum of two specified times using TimeSpan

Here, we are going to learn how to find the sum of two specified times using TimeSpan in C#?
Submitted by Nidhi, on October 03, 2020

Here, we will find the sum of two specified times using TimeSpan class and store the result into the object of TimeSpan class and print the values of the hour, minute, and seconds using TimeSpan class.

Program:

The source code to find the sum of two specified times using the TimeSpan class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the sum of two specified times using TimeSpan.

using System;

class Demo
{
    static void Main()
    {
        TimeSpan time;

        TimeSpan ts1 = new TimeSpan(10, 20, 50);
        TimeSpan ts2 = new TimeSpan(8, 19, 32);

        time = ts1 + ts2;

        Console.WriteLine("Hours:{0}, Minutes:{1}, Seconds:{2}",time.Hours,time.Minutes,time.Seconds);
    }
}

Output:

Hours:18, Minutes:40, Seconds:22
Press any key to continue . . .

Explanation:

Here, we created a class Demo that contains the Main() method. In the Main() method, we created the two time-spans with a specified time.

time = ts1 + ts2;

Using the above statement, we calculated the sum with two time-span and then we printed the result on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.