Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows time
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 14-Feb-13 19:32pm
Rate this: bad
good
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 [^]
  Permalink  
Comments
Manoj Chamikara at 15-Feb-13 1:55am
   
CS2011 I need sum of time in same date anyway thank you CS2011
CS2011 at 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 at 15-Feb-13 2:18am
   
Thanks dude It's Works Thanks a lot again it very helpful
Rate this: bad
good
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
http://stackoverflow.com/questions/510778/add-or-sum-of-hours-like-1330000020-133020-but-how[^]
  Permalink  
Comments
Manoj Chamikara at 15-Feb-13 1:58am
   
vinodkumarnie my time comes from separated 3 variables how should i apply for this ?
vinodkumarnie at 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 at 15-Feb-13 2:23am
   
Thanks for the help Vinod kumarnie
vinodkumarnie at 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)

  Print Answers RSS
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


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