Here is my code:
TimeSpan span1 = TimeSpan.FromHours(dtmIn.Value.Hour);
TimeSpan span2 = TimeSpan.FromHours(dtmOut.Value.Hour);
TimeSpan span3 = TimeSpan.FromMinutes(dtmIn.Value.Minute);
TimeSpan span4 = TimeSpan.FromMinutes(dtmOut.Value.Minute);
TimeSpan span5 = span2.Subtract(span1) + span4.Subtract(span3);
lblTotal.Text = Convert.ToDecimal(span5).ToString("#.00");
I get a formatting error for the last line. I can get the label to return as time obviously, but I need it to return as a decimal.
time2=DateTime. Parsee(10:00AM); TimeSpan ts = time2. Subtract(time1);//ex.. ts= 10.00.
A TimeSpan value can be represented as [-]d. hh:mm:ss. ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. The value of the Hours property is the hours component, hh.
It doesn't make sense to convert a TimeSpan
to a decimal, becuase time isn't numeric.
You probably want span5.TotalHours
, which is a double
that includes the fractional portion.
What is the decimal supposed to represent? Hours and fractions of an hour? You must understand that TimeSpan
can't infer this, as TimeSpan
could span milliseconds or millennia.
If in fact hours and fractions of an hour, then:
lbTotal.Text = span5.TotalHours.ToString("#.00");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With