【问题标题】:C# Timer counter in xx.xx.xx formatxx.xx.xx 格式的 C# Timer 计数器
【发布时间】:2012-04-18 04:14:26
【问题描述】:

我有一个计数器每 1 秒计数一次,并将 1 添加到 int。

问题
如何格式化我的字符串,使计数器看起来像这样:

00:01:23

代替:

123

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

for (int i = 0; i < 1; i++)
        {
            _Counter += 1;
            labelUpTime.Text = _Counter.ToString();
        }

我的计时器间隔设置为:1000(因此它每秒增加 1)。
我确实读过一些关于 string.Format("") 的内容,但我不知道它是否适用。
谢谢你能指导我完成这个:D!

【问题讨论】:

  • 将 123 秒变成“1:23”会很困难。考虑“2:03”。

标签: c# timer formatting int


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    TimeSpan timer = new TimeSpan(0);
    

    在你的时间间隔内:

    timer += TimeSpan.FromSeconds(1);
    

    【讨论】:

      【解决方案3】:

      不要使用计数器,也不要依赖计时器每秒钟精确一次。它不会。做这样的事情。

      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();
          }
      }
      

      您可以使用TimeSpan.ToString 方法调整格式。

      【讨论】:

        【解决方案4】:

        使用时间跨度:

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

        【讨论】:

          【解决方案5】:

          使用时间跨度。添加第二次使用

          mytimespan.Add(new TimespanFromSeconds(1));
          Console.WriteLine(mytimespan);    //Output in the form of xx:xx:xx
          

          http://www.dotnetperls.com/timespan

          【讨论】:

            【解决方案6】:

            对我来说效果很好

                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();
                    }
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-10-31
              • 2021-09-25
              相关资源
              最近更新 更多