Hi,
I have a problem in a project , in this project I used intime and outtime as two fields(Datetime pickers), on outtime textchanged event it had to calcualte outtime - intime - 30min(Lunchtime) and display it in a text box.In case if the employee works more than 8:30 hours .For example 12 hours than it has to calculate like Extra hours = Total hours worked - (min hours woked +lunch hours) eg., extrahours 12 - (8:30+30) -> 12 - 9 -> 3 hours , then in another text box it has to display 3 hours as extra hours worked.
I used double and time spans buts its not working out, please help me out.
String tot = String.Empty;
TimeSpan ts = new TimeSpan();
ts = dtpouttime.Value.TimeOfDay - dtpintime.Value.TimeOfDay;
txtworkedhours.Text = ts.ToString();
txtworkedhours.Text = Convert.ToDouble(Convert.ToDouble(ts.Hours - 0.50) + (Convert.ToDouble(ts.Minutes) / 60.00)).ToString("#.00");
Thanks in Advance,
Prasad
Loading
AlanPosted Aug 5, 2008, 6:38 AM
OK, I think I'm clear now:
TimeSpan ts = dtpouttime.Value.TimeOfDay - dtpintime.Value.TimeOfDay;
ts = ts.Subtract(TimeSpan.FromMinutes(30)); // subtract 30 mins for lunch
if (ts.TotalHours >= 8.5)
{
txtworkedhours.Text = "08:30:00";
txtworkedExtrahours = ts.Subtract(TimeSpan.FromHours(8.5)).ToString();
}
else
{
txtworkedhours.Text = ts.ToString();
txtworkedExtrahours = TimeSpan.Zero.ToString();
}
durga prasadPosted Aug 5, 2008, 12:46 AM
So in one textbox it should calculate the minimum working hours from the datetimepickers (8:30 +30min) then in the next textbox it should calculate
extra hours worked = total hours worked - min working hours.
durga prasadPosted Aug 5, 2008, 12:45 AM
So in one textbox it should calculate the minimum working hours from the datetimepickers (8:30 +30min) then in the next textbox it should calculate
extra hours worked = total hours worked - min working hours.
AlanPosted Aug 4, 2008, 6:27 AM
Not sure whether I've understood that correctly, but try:
TimeSpan ts = new TimeSpan();
ts = dtpouttime.Value.TimeOfDay - dtpintime.Value.TimeOfDay;
ts = ts.Subtract(TimeSpan.FromMinutes(30)); // subtract 30 mins for lunch
txtworkedhours.Text = ts.ToString();
if (ts.TotalHours > 12)
{
txtworkedExtrahours = ts.Subtract(TimeSpan.FromHours(12)).ToString();
}
else
{
txtworkedExtrahours = TimeSpan.Zero.ToString();
}