Click here to Skip to main content
11,928,786 members (47,711 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Windows time
How to calculate two different time in C# (windows)
Time 1 - 11:10:40
Time 2 - 00:12:10
Sum - 11:22:50

Thank in advance
Posted 14-Feb-13 20:32pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you mean adding two dates this will help

System.DateTime today = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0);
System.DateTime answer = today.Add(duration);
System.Console.WriteLine("{0:dddd}", answer);

you can find more details about this in links below

TimeSpan Structure[^]

DateTime.Add Method [^]
Manoj Chamikara 15-Feb-13 1:55am
CS2011 I need sum of time in same date anyway thank you CS2011
CS2011 15-Feb-13 2:03am
if i understand you i think you need to add some Hr or min or second in the current time (or any time). TimeSpane has const for that.
for example if want to add Hr,min and sec you can use
TimeSpan(Int32, Int32, Int32) = Initializes a new instance of the TimeSpan structure to a specified number of hours, minutes, and seconds. and pass that to Add method. Hope this helps
Manoj Chamikara 15-Feb-13 2:18am
Thanks dude It's Works Thanks a lot again it very helpful
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you choose to use the TimeSpan, be aware about the Days part:

TimeSpan t1 = TimeSpan.Parse("23:30");
TimeSpan t2 = TimeSpan.Parse("00:40:00");
TimeSpan t3 = t1.Add(t2);
Label1.Text = t3.ToString(); //to bind to label
Console.WriteLine(t3); // 1.00:10:00

With DateTime:
DateTime d1 = DateTime.Parse("23:30");
DateTime d2 = DateTime.Parse("00:40:00");
DateTime d3 = d1.Add(d2.TimeOfDay); 
Label2.Text = t3.ToString(); //to bind to label
Console.WriteLine(d3.TimeOfDay); // 00:10:00

refer below link[^]
Manoj Chamikara 15-Feb-13 1:58am
vinodkumarnie my time comes from separated 3 variables how should i apply for this ?
vinodkumarnie 15-Feb-13 2:11am
Its very simple.
TimeSpan t1 = TimeSpan.Parse(variable1);
TimeSpan t2 = TimeSpan.Parse(variable2);
TimeSpan t3 = t1.Add(t2);
Label1.Text = t3.ToString(); //to bind to label
Console.WriteLine(t3); // 1.00:10:00

if answer is correct Accept the solution..
Manoj Chamikara 15-Feb-13 2:23am
Thanks for the help Vinod kumarnie
vinodkumarnie 15-Feb-13 2:30am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100