Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting two different DateTimes and trying to calculate the difference between them in hours and write it to a label.

C#
label_NetTime.Text = "00:00";
label_Total.Text = "£0.00";

DateTime StartTime;
DateTime EndTime;

StartTime = (DateTime)(timeEdit_StartTime.EditValue);
EndTime = (DateTime)(timeEdit_EndTime.EditValue);

DateTime StartTimeToUse = DateTime.Now.Date;
DateTime EndTimeToUse = DateTime.Now.Date;

DateTime TotalTime = DateTime.MinValue;

StartTimeToUse = dateEdit_DateOfWork.DateTime.Date;
StartTimeToUse = StartTimeToUse.AddHours(StartTime.Hour);
StartTimeToUse = StartTimeToUse.AddMinutes(StartTime.Minute);

EndTimeToUse = (DateTime)(dateEdit_NightShiftEndDate.EditValue);
EndTimeToUse = EndTimeToUse.AddHours(timeEdit_NightShiftEndTime.Time.Hour);
EndTimeToUse = EndTimeToUse.AddMinutes(timeEdit_NightShiftEndTime.Time.Minute);

if (StartTimeToUse.CompareTo(EndTimeToUse) <= 0)
{
    System.TimeSpan diffResult = EndTimeToUse.Subtract(StartTimeToUse);

    if (diffResult.TotalMinutes > 0)
    {
        if ((diffResult.TotalMinutes - numbericTextBox_BreakMins.GetNumericValue()) >= 0)
        {
            TotalTime = TotalTime.AddMinutes(diffResult.TotalMinutes);
            TotalTime = TotalTime.AddMinutes(-numbericTextBox_BreakMins.GetNumericValue());
        }
    }

    label_NetTime.Text = TotalTime.ToString("HH:mm");


What I have tried:

Currently the label is displaying the wrong value. An issue with it seems to be that both "AddHours" and "AddMinutes" don't seem to be adding anything to the StartTimeToUse variable.

Edit:

The issue with the code is here:

C#
StartTime = (DateTime)timeEdit_StartTime.EditValue;
EndTime = (DateTime)timeEdit_EndTime.EditValue;


These variables aren't getting the hour and minute values from the TimeEdit. I've tried it as .Time instead of .EditValue but that doesn't work.
Posted
Updated 25-May-22 1:04am
v2
Comments
Richard MacCutchan 25-May-22 6:41am    
Both AddHours and AddMinutes are well used library methods that work. Whatever values you are passing to those methods must be the issue. Use your debugger to see exactly what is happening. In particular check that StartTime actually contains non-zero values for Hour and Minute.
Will Sewell 25-May-22 6:54am    
Thank you yes. I've just seen that StartTime and EndTime contain zero values.

1 solution

Both the AddHours and AddMinutes methods work fine - they are old methods that have worked for years, to it's very unlikely t5hat a total failure of that form would happen.

But I'd still use a Timespan, as I suggested yesterday when you asked a very similar question: How do I best update a datetime variable from two separate time/date edit boxes?[^]

And I'd use the debugger to find out exactly what values I was working with instead of assuming it's a .NET Framework error!
 
Share this answer
 
Comments
Will Sewell 25-May-22 7:17am    
"And I'd use the debugger to find out exactly what values I was working with instead of assuming it's a .NET Framework error!" I realised this as soon as the first comment came in.

Thanks for your help again Griff. I did try and use TimeSpan in the posted logic but it wasn't hitting that point with the right data.

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