Search code examples
c#wpfdatetimetimespanduration

Remove Minutes from a Timespan duration


I know this has been asked before but i couldn't figure out how to get the answers given working for my particular example. This is a WPF application written in C# and i'm trying to remove a number of minutes from a timespan.

So far i've got the application to figure out the duration by removing a Start Time from a Finish Time, but what i'm trying to do now is remove the number of minutes the user has entered into the form.

Here's the code so far

 private void testcal_Click(object sender, EventArgs e)
    {

        string startTime = teststart.Text;
        string finishTime = testfinish.Text;

        // Trying to deduct this lunchTime var from the duration TimeSpan
        string lunchTime = testlunch.Text;

        TimeSpan duration = DateTime.Parse(finishTime).Subtract(DateTime.Parse(startTime));
        testlabel.Text = duration.ToString(@"hh\:mm");

    }

Edit - Updated to include private void


Solution

  • With user input it would be.

    string startTime = teststart.Text;
    string finishTime = testfinish.Text;
    string lunchTime = testlunch.Text;
    
    TimeSpan duration = DateTime.Parse(finishTime).Subtract(DateTime.Parse(startTime)).
        Subtract(TimeSpan.FromMinutes(Int32.Parse(lunchtime)));