Time time = new Time();
        private void timer1_Tick(object sender, EventArgs e)
        {
            time++;
            this.lblTime.Text = time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
        }以上代码,获得时间的格式是1:1:1
但我想获得时间的格式是:01:01:01,
就是说,单个数字时,前面加个零
请问要怎么写?

解决方案 »

  1.   

    this.lblTime.Text = time.ToString("HH:mm:ss");
      

  2.   

    Time.ToString("HH:mm:ss")或"hh:mm:ss",看你需要24时还是12时的表示
      

  3.   

    请参考:        private void timer1_Tick(object sender, EventArgs e)
            {
                DateTime time = System.DateTime.Now;
                this.lblTime.Text = time.ToString("hh:mm:ss");
            }
      

  4.   

    Time time = new Time();
            private void timer1_Tick(object sender, EventArgs e)
            {
                time++;
                this.lblTime.Text = time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
            }
    C#中没有Time类型啊,是不是楼主自定义的类,如果是自定义的类就需要在类中自己重写GetHours(),GetMinutes() 和 GetSeconds() 方法
      

  5.   

    LZ用的第三方类库?如果重写了ToString()支持格式输出的话就用time.ToString("HH:mm:ss"),如果没有就
    time.GetHours().ToString("00") + ":" + time.GetMinutes.ToString("00") + ":" + time.GetSeconds.ToString("00")
      

  6.   

    呵呵 time是不是datetime类型的变量?
    不过还是学习了 自己试一下
      

  7.   

    呵呵         private void timer1_Tick(object sender, EventArgs e)
            {
                label1.Text = DateTime.Now.ToString(":HH:mm:ss"); 
            }
    自己试了一下 可以的
      

  8.   

    Time应该是提供获取时间的那啥玩意把。
      

  9.   

    Time是你自己定义的类型?this.lblTime.Text = time.GetHours().ToString("00") + ":" + time.GetMinutes().ToString("00") + ":" + time.GetSeconds().ToString("00");
      

  10.   

    Time是LZ在网上找的一个类,我也找过,哈哈
      

  11.   

    重在学习,我也来一个:
     public string Time_Format(string NDate)
            {
                string sh, sm, se;
                int hh, mm, ss;
                try
                {
                    hh = Convert.ToDateTime(NDate).Hour;
                    mm = Convert.ToDateTime(NDate).Minute;
                    ss = Convert.ToDateTime(NDate).Second;
                    
                }
                catch
                {
                    return "";
                }
                sh = Convert.ToString(hh);
                if (sh.Length < 2)
                    sh = "0" + sh;
                sm = Convert.ToString(mm);
                if (sm.Length < 2)
                    sm = "0" + sm;
                se = Convert.ToString(ss);
                if (se.Length < 2)
                    se = "0" + se;
                return sh + sm + se;
            }
      

  12.   

    这是我之前写的笨方法            if (time.GetHours().ToString().Length == 1)
                {
                    hour = "0" + time.GetHours().ToString();
                }
                else
                {
                    hour =time.GetHours().ToString();
                }            if (time.GetMinutes().ToString().Length == 1)
                {
                    Minutes = "0" + time.GetMinutes().ToString();
                }
                else
                {
                    Minutes =time.GetMinutes().ToString();
                }            if (time.GetSeconds().ToString().Length == 1)
                {
                    Seconds = "0" + time.GetSeconds().ToString();
                }
                else
                {
                    Seconds = time.GetSeconds().ToString();
                }
      

  13.   

    time是我从网上找的的类,忘了说了,发代码,是一个记时器来的.    class Time
        {
              private int hours;  //小时
              private int minutes; //分钟
              private int seconds; //秒钟
              
              public Time()
              {
               this.hours = 0;
               this.minutes = 0;
               this.seconds = 0;
              }          public Time(int hours,int minutes,int seconds)
              {
               this.hours = hours;
               this.minutes = minutes;
               this.seconds = seconds;
              }          public void SetHours(int hours)
              {
               this.hours = hours;
              }          public void SetMinutes(int minutes)
              {
               this.minutes = minutes;
              }          public void SetSeconds(int seconds)
              {
               this.seconds = seconds;
              }          public int GetHours()
              {
               return this.hours;
              }          public int GetMinutes()
              {
               return this.minutes;
              }          public int GetSeconds()
              {
               return this.seconds;
              }          public static Time operator ++(Time time)
              {
               time.seconds++;
               if (time.seconds >= 60)
               {
                time.minutes++;
                time.seconds = 0;
                if (time.minutes >= 60)
                {
                 time.hours++;
                 time.minutes = 0;
                 time.seconds = 0;
                 if (time.hours >= 24)
                 {
                  time.hours = 0;
                  time.minutes = 0;
                  time.seconds = 0;
                 }
                }
               }
               return new Time(time.hours,time.minutes,time.seconds);
              }                    this.timer1.Start();
                        //1000等于1秒
                        this.timer1.Interval = 1000;
      

  14.   

    DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")
      

  15.   

    24小时的用HH:mm:ss;12小时用hh:mm:ss