Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 582
HWGMousey Avatar asked Aug 31 '25 01:08

HWGMousey


2 Answers

Are you asking how to subtract minutes from a TimeSpan?

If so try something like

TimeSpan ts = new TimeSpan().Subtract(TimeSpan.FromMinutes(30));
like image 182
Brian Mitchell Avatar answered Sep 02 '25 13:09

Brian Mitchell


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)));
like image 20
weirdev Avatar answered Sep 02 '25 15:09

weirdev