Click here to Skip to main content
11,702,890 members (62,144 online)
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 Maciej Los 415
1 Sergey Alexandrovich Kryukov 325
2 OriginalGriff 260
3 CHill60 120
4 Andy Lanng 119
0 OriginalGriff 8,379
1 Sergey Alexandrovich Kryukov 7,587
2 CPallini 4,947
3 Maciej Los 4,501
4 Mika Wendelius 3,566


Advertise | Privacy | Mobile
Web01 | 2.8.150819.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