重在学习,我也来一个: 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; }
{
DateTime time = System.DateTime.Now;
this.lblTime.Text = time.ToString("hh:mm:ss");
}
private void timer1_Tick(object sender, EventArgs e)
{
time++;
this.lblTime.Text = time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
}
C#中没有Time类型啊,是不是楼主自定义的类,如果是自定义的类就需要在类中自己重写GetHours(),GetMinutes() 和 GetSeconds() 方法
time.GetHours().ToString("00") + ":" + time.GetMinutes.ToString("00") + ":" + time.GetSeconds.ToString("00")
不过还是学习了 自己试一下
{
label1.Text = DateTime.Now.ToString(":HH:mm:ss");
}
自己试了一下 可以的
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;
}
{
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();
}
{
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;