110,566
社区成员
发帖
与我相关
我的任务
分享
Time time = new Time();
private void timer1_Tick(object sender, EventArgs e)
{
time++;
this.lblTime.Text = time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
}
DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")
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;
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();
}
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;
}