Search code examples
c#.netwinformstimerstring-formatting

Remove specified multiples of 60 from a number?


So I'm trying to make a Stopwatch program and I can't figure out how to format the seconds and mintues correctly because every minute or 60 minutes, it doesnt reset the seconds/minutes that are displayed back to 0. I've been searching for something to just simply format the seconds/minutes in a way that makes it looks normal.

My code so far (it's just a snippet):

updater.Tick += (sender, e) =>
{
    hours = (int)stopwatch.Elapsed.TotalHours;
    minutes = (int)stopwatch.Elapsed.TotalMinutes;
    seconds = (int)stopwatch.Elapsed.TotalSeconds;

    if (hours > 24)
    {
        MessageBox.Show("Time limit exceeded. Please reset Stopwatch to continue.", "Time Limit Exceeded", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    lblHours.Text = string.Format("{0:D2}", hours);
    lblMinutes.Text = string.Format("{0:D2}", minutes);
    lblSeconds.Text = string.Format("{0:D2}", seconds);

    Application.DoEvents();
};

If someone could help me figure this out, then I would really appreciate it.


Solution

  • You don't want TotalMinutes and TotalSeconds, use Minutes and Seconds instead.