xx.xx.xx格式的C#定时器计数器

xx.xx.xx格式的C#定时器计数器,c#,timer,formatting,int,C#,Timer,Formatting,Int,我有一个计数器,它每1秒计数一次,然后把1加到整数上 问题 如何格式化我的字符串,使计数器如下所示: 00:01:23 而不是: 123 我尝试过的事情 到目前为止我已经尝试过的事情: for (int i = 0; i < 1; i++) { _Counter += 1; labelUpTime.Text = _Counter.ToString(); } for(int i=0;i

我有一个计数器,它每1秒计数一次,然后把1加到整数上

问题
如何格式化我的字符串,使计数器如下所示:

00:01:23

而不是:

123

我尝试过的事情
到目前为止我已经尝试过的事情:

for (int i = 0; i < 1; i++)
        {
            _Counter += 1;
            labelUpTime.Text = _Counter.ToString();
        }
for(int i=0;i<1;i++)
{
_计数器+=1;
labeloptime.Text=_Counter.ToString();
}
我的计时器的时间间隔设置为:1000(因此它每秒增加1)。
我确实读过一些关于string.Format(“”)的内容,但我不知道它是否适用。

谢谢,如果你能引导我通过这个:D

您可以将其设置为
时间跨度
(因为它是一个时间跨度),然后设置格式:

labelUpTime.Text = TimeSpan.FromSeconds(_Counter).ToString();
在你的休息时间:

timer += TimeSpan.FromSeconds(1);

不要使用计数器,也不要依赖计时器每秒精确地触发。不会的。像这样做

class TimerTest
{
    private DateTime _start = DateTime.Now;
    private Timer _timer = new Timer(1000);

    public TimerTest()
    {
        // (DateTime.Now - _start) returns a TimeSpan object
        // Default TimeSpan.ToString() returns 00:00:00
        _timer.Elapsed = (o, e) => labelUpTime.Text = (DateTime.Now - _start).ToString();
    }
}
您可以使用该方法调整格式。

使用时间跨度:

_Counter += 1;
labelUpTime.Text = TimeSpan.FromSeconds(_Counter).ToString();

使用timespan。添加第二个用途

mytimespan.Add(new TimespanFromSeconds(1));
Console.WriteLine(mytimespan);    //Output in the form of xx:xx:xx
这对我来说很管用

    public TimeSpan ElapsedTimeFormatted
    {
        get
        {
            if (FinishedOn != null &&
                StartedAt != null)
            {
                TimeSpan durationCount = new TimeSpan();

                int hours = 0;
                int minutes = 0;
                int seconds = 0;

                var times = Segments.Select(c => c.ElapsedTimeFormatted).ToList();

                foreach (var time in times)
                {
                    TimeSpan timeParse = TimeSpan.Parse(time);

                    hours = hours + (int)timeParse.Hours;
                    minutes = minutes + (int)timeParse.Minutes;
                    seconds = seconds + (int)timeParse.Seconds;

                    durationCount = new TimeSpan(hours, minutes, seconds);
                }

                return durationCount;
            }

            return new TimeSpan();
        }
    }
把123秒变成“1:23”是很困难的。考虑“2 03”。
    public TimeSpan ElapsedTimeFormatted
    {
        get
        {
            if (FinishedOn != null &&
                StartedAt != null)
            {
                TimeSpan durationCount = new TimeSpan();

                int hours = 0;
                int minutes = 0;
                int seconds = 0;

                var times = Segments.Select(c => c.ElapsedTimeFormatted).ToList();

                foreach (var time in times)
                {
                    TimeSpan timeParse = TimeSpan.Parse(time);

                    hours = hours + (int)timeParse.Hours;
                    minutes = minutes + (int)timeParse.Minutes;
                    seconds = seconds + (int)timeParse.Seconds;

                    durationCount = new TimeSpan(hours, minutes, seconds);
                }

                return durationCount;
            }

            return new TimeSpan();
        }
    }