Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to calculate two different time in C# (windows)
Ex-
Time 1 - 11:10:40
Time 2 - 00:12:10
Sum - 11:22:50

Thank in advance
Posted

If you mean adding two dates this will help

C#
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 [^]
 
Share this answer
 
Comments
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
If you choose to use the TimeSpan, be aware about the Days part:

C#
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
http://stackoverflow.com/questions/510778/add-or-sum-of-hours-like-1330000020-133020-but-how[^]
 
Share this answer
 
Comments
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);
variable3=t3.ToString();
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    
welcome

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900