Adding 2 times of Format HH:mm:ss using timespan

Posted by saisuresh074-17816 under ASP.NET on 10/16/2012 | Points: 10 | Views : 4745 | Status : [Member] | Replies : 2
hi guys

I m trying to add two times of format HH:mm:ss using timespan..and problem is after hours crossing 24 hrs it is giving me as 1 day but i need only in hrs basis...so help me out..i wrote code as


string hour1 = lblworking_hoursweek.Text;
string hour2 = Totcalc;

DateTime dt1 = DateTime.Parse(hour1);
DateTime dt2 = DateTime.Parse(hour2);

TimeSpan ts1 = new TimeSpan(dt1.Hour, dt1.Minute, dt1.Second);
TimeSpan ts2 = new TimeSpan(dt2.Hour, dt2.Minute, dt2.Second);
TimeSpan sum = (ts1 + ts2);
Totcalc = Convert.ToString(sum);

So my sum should be only in hrs ..

example:
i should get
12:10:00 + 12:00:00 =24:10:00

its giving me
12:10:00 + 12:00:00 =1:00:10:00




Responses

Posted by: Santhi on: 10/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

First get the hour1 in a string and minutes an another string.
Example:
string hour1= 12,
string hour1minutes = 00,
Then use timespan for this
Timespan tshour1 = new timespan(hour1,hour1minutes,00)
Then declare a string called
FirstTotalHrs = Math.Floor(tshour1 .TotalHours) + ":" + (tshour1 .Minutes < 10 ? "0" + tshour1 .Minutes : tshour1 .Minutes.ToString());


Like follow the above steps to get the second time(i.e., hour2)
string hour2= 12,
string hour2minutes = 00,
Then use timespan for this
Timespan tshour2 = new timespan(hour2,hour2minutes,00)
Then declare a string called
SecondTotalhours= Math.Floor(tshour2.TotalHours) + ":" + (tshour2.Minutes < 10 ? "0" + tshour2 .Minutes : tshour2 .Minutes.ToString());

Then to sum these first time value and the second time value:



string[] strTotalWorkedHours = FirstTotalHrs.Split(':');
string strFirstWorkingHours = strTotalWorkedHours[0];
string strFirstWorkingMinutes = strTotalWorkedHours[1];
string[] strLeaveHours = SecondTotalhours.Split(':');
string strFirstLeavehours = strLeaveHours[0];
string strFirstLeaveMinutes = strLeaveHours[1];
TimeSpan tsHours = new TimeSpan(0, 0, 0);
tsHours = TimeSpan.FromHours(Convert.ToInt32(strFirstWorkingHours) + Convert.ToInt32(strFirstLeavehours));
TimeSpan tsMinutes = new TimeSpan(0, 0, 0);
tsMinutes = TimeSpan.FromMinutes(Convert.ToInt32(strFirstWorkingMinutes) + Convert.ToInt32(strFirstLeaveMinutes));
string strTotalHours = Math.Floor(tsHours.TotalHours + tsMinutes.TotalHours) + ":" + (tsMinutes.Minutes < 10 ? "0" + tsMinutes.Minutes : tsMinutes.Minutes.ToString() + ":" + (tsHours.Seconds < 10 ? "0" + tsHours.Seconds : tsHours.Seconds.ToString()));


This will give you the sum of HH:MM:SS.

Thanks && Regards,
V. Santhi




Thanks & Regards,
Santhi .V

saisuresh074-17816, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: saisuresh074-17816 on: 10/16/2012 [Member] Starter | Points: 25

Up
0
Down
@V. Santhi :Thank you....


saisuresh074-17816, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response