Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir..
I have two Dropdown List.Dropdown1 is for start Time and Dropdown2 is for end time.I have to calculate time.That I have done using timespan.now i want calculate total of time so how can i code for that.
I will share my code so you will understand.

C#
decimal time = 0;
DateTime time1 = new DateTime();
DateTime time2 = new DateTime();
time1 = DateTime.Parse(ddl1.SelectedItem.Text);//ex..time1=DateTime.Parse(8:00AM);
time2 = DateTime.Parse(ddl2.SelectedItem.Text);//ex.. time2=DateTime.Parsee(10:00AM);
TimeSpan ts = time2.Subtract(time1);//ex.. ts= 10.00.Subtract(8:00) so ts=2:00
ll1.Text = Convert.ToString(ts);//ex.. ll1.Text=2:00;
//this two line i dont have idea it is right or wrong.
time = time + ts;//Error Cannot use + operator Decimal to System.TimeSpan
Label2.Text = Convert.ToString(time);


Suppose..
I have Loop so agian this process work.
so first Time ll1.Text=2:00
Secong Time ll1.Text=2:30;
Now I want to calculate total time 2:00+2:30 =4:30 and store into another label .

Please Anu body have code for this solution plz help me for this.
Posted
Updated 3-Dec-15 0:18am
v2

Why don't you define your time variable as a TimeSpan?
C#
TimeSpan time = TimeSpan.Zero;
// ...
time += ts;
Label2.Text = time.ToString();

would work.
And better use the ToString() method; Convert class should be avoided when possible.
 
Share this answer
 
Comments
Member 12097108 3-Dec-15 6:33am    
Hello This code I have Implemented.
This is store last value not calculate value
phil.o 3-Dec-15 6:49am    
Sorry, I cannot understand this, which does not make much sense.
Member 12097108 3-Dec-15 6:57am    
According toMy Earlier code
ts=2:00 .again we select time from dropdown list so we get ts=1:30
now I want 2:00+1:30=3:30
but according to your code
It will print Last value Like 1:30. not calculate them.
phil.o 3-Dec-15 7:03am    
No. The line time += ts; is equivalent to time = time + ts;.
Member 12097108 3-Dec-15 7:15am    
Thank you Sir
I have done this.
It was my mistake...Thank you Verymuch sir
Try with below code:
C#
DateTime time1 = new DateTime();
DateTime time2 = new DateTime();

// 1st time dropdown selection
time1 = DateTime.Parse("8:00AM");
time2 = DateTime.Parse("10:00AM");
TimeSpan ts = time2.Subtract(time1); // 02:00:00

// 2nd time dropdown selection
time1 = DateTime.Parse("8:00AM");
time2 = DateTime.Parse("9:30AM");
TimeSpan ts1 = time2.Subtract(time1); // 01:30:00

TimeSpan tsTotal = ts + ts1;

string t1 = Convert.ToString(tsTotal); // 03:30:00
Label2.Text = t1;
 
Share this answer
 
v2

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