【问题标题】:Convert minutes to hours, minutes and seconds [closed]将分钟转换为小时、分钟和秒[关闭]
【发布时间】:2012-07-13 04:57:32
【问题描述】:

在我的界面中,我让用户输入他们可以暂停某项操作的 X 分钟。

如何将其转换为小时、分钟和秒?

我需要它来更新倒计时标签以向用户显示剩余时间。

【问题讨论】:

  • 我只能将其转换为一种格式,但我需要计算小时、分钟和秒
  • 用户输入的格式是什么?
  • 格式以分钟为单位
  • TimeSpan span = TimeSpan.FromMinutes(Convert.ToDouble(TotalTime));字符串标签 = Convert.ToInt32(span.TotalHours).ToString()+":"+span.Minutes;返回标签;

标签: c# timer


【解决方案1】:

首先创建 TimeSpan,然后将其格式化为您想要的任何格式:

TimeSpan span = TimeSpan.FromMinutes(minutes);
string label = span.ToString(@"hh\:mm\:ss");

【讨论】:

    【解决方案2】:

    创建一个新的TimeSpan

    var pauseDuration = TimeSpan.FromMinutes(minutes);
    

    您现在拥有方便的属性HoursMinutesSeconds。我应该认为它们是不言自明的。

    【讨论】:

      【解决方案3】:

      这应该给你一个开始的地方。定时器设置为 1000 毫秒。它使用与其他答案相同的想法,但更加充实。

      public partial class Form1 : Form
      {
          TimeSpan duration;
      
          public Form1()
          {
              InitializeComponent();
          }
      
          private void timer1_Tick(object sender, EventArgs e)
          {
            duration  =  duration.Subtract(TimeSpan.FromSeconds(1)); //Subtract a second and reassign
            if (duration.Seconds < 0)
            {
                timer1.Stop();
                return;
            }
      
            lblHours.Text = duration.Hours.ToString();
            lblMinutes.Text = duration.Minutes.ToString();
            lblSeconds.Text = duration.Seconds.ToString();
          }
      
          private void button1_Click(object sender, EventArgs e)
          {
              if(!(string.IsNullOrEmpty(textBox1.Text)))
              {
                  int minutes;
                  bool result = int.TryParse(textBox1.Text, out minutes);
                  if (result)
                  {
                      duration = TimeSpan.FromMinutes(minutes);
                      timer1.Start();
                  }
              }
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-07-18
        • 2015-05-18
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 1970-01-01
        • 2015-06-02
        相关资源
        最近更新 更多