.net - How to add two time span values having negative sign in c#? -


i have 2 columns in table i-e working hours , hours employee attendance , total time employee has work 9 (9) hours if check in @ 9 o ' clock , check out @ 11 o ' clock system automatically calculate his/her working hours , hours in case working hours 2 hours , hours in negative form e.g -7:00:00 hours because employee leave 7 hours before actual time , stored negative value "varchar" in database user facilitation understand either employee worked more given time or less.now need add these hours of employee creating summary of month don't know how add these negative time span , when convert timeofday format system cannot convert due negative sign.

i calculate sum of working hours after reading database as:

wrkhrs=wrkhrs.add(convert.todatetime(dr["workhrs"].tostring()).timeofday); 

and found code subtraction as:

timespan exthrs = org_work.subtract(tot_work); 

but if timespan have negative sign don't work. if body have idea kindly share . in advance.

edit:

public tuple<string,string>  calculate_hours(int id,datetime strt, datetime end)         {             timespan wrkhrs = new timespan(0, 0, 0);             timespan exthrs=new timespan(0,0,0);             //select * vw_rept_attend userid ='" + id + "' , convert(date,atndate) between '" + strt.date + "' , '" + end.date + "'             cmd = new sqlcommand("sp_calculate_hours_for_report", conn);             cmd.commandtype = commandtype.storedprocedure;             cmd.parameters.addwithvalue("@id", id);             cmd.parameters.addwithvalue("@startdate", strt.date);             cmd.parameters.addwithvalue("@end", end.date);             conn.open();             dr = cmd.executereader();             while (dr.read())             {                 if (dr["workhrs"].tostring().length>0)                    wrkhrs=wrkhrs.add(convert.todatetime(dr["workhrs"].tostring()).timeofday);                if (!dr["extrahrs"].tostring().contains("-") && dr["extrahrs"].tostring().length > 0)                 {                     exthrs = exthrs.add(convert.todatetime(dr["extrahrs"].tostring()).timeofday);                 }                 else if (dr["extrahrs"].tostring().contains("-") && dr["extrahrs"].tostring().length > 0)                 {                     string ext = dr["extrahrs"].tostring().substring(dr["extrahrs"].tostring().lastindexof("-") +1);                     exthrs = exthrs.subtract(convert.todatetime(ext).timeofday);                 }             }             conn.close();             dr.close();            return new tuple<string, string>(string.format("{0:00}:{1:00}:{2:00}", (int)timespan.parse(wrkhrs.tostring()).totalhours, math.abs((int)timespan.parse(wrkhrs.tostring()).minutes), math.abs((int)timespan.parse(wrkhrs.tostring()).seconds)), string.format("{0:00}:{1:00}:{2:00}", (int)timespan.parse(exthrs.tostring()).totalhours, math.abs((int)timespan.parse(exthrs.tostring()).minutes), math.abs((int)timespan.parse(exthrs.tostring()).seconds)));          } 

and called out put of above function as:

var mytuple = calculate_hours(id,convert.todatetime(dtstart.text), convert.todatetime(dtend.text)); string tot_workhrs= mytuple.item1; string tot_exthrs=  mytuple.item2; 

above logic retrieve correct output, if has question please feel free ask me creating new comment on post.

hope helpful all....

1. use timespan.duration():

https://msdn.microsoft.com/en-us/library/system.timespan.duration(v=vs.110).aspx

"returns new timespan object value absolute value of current timespan object"

2. or code here:

static string tohmstring(timespan timespan) {      if (timespan.ticks < 0) return "-" + tohmstring(timespan.negate());      return timespan.totalhours.tostring("#0") + ":" + timespan.minutes.tostring("00"); }  console.writeline(tohmstring(timespan.fromhours(3)));       //prints "3:00" console.writeline(tohmstring(timespan.fromhours(-27.75)));  //prints "-28:45" 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -