string hour = this.numericUpDown1.Value.ToString();//获取“时间定时”区域的小时
string minute = this.numericUpDown2.Value.ToString();//获取“时间定时”区域的分钟
string second = this.numericUpDown3.Value.ToString(); //获取“时间定时”区域的秒
int hour1 = Convert.ToInt32(hour);//将string类型的小时转换为int型的小时
int minute1 = Convert.ToInt32(minute);//将string类型的分钟转换为int型的分钟
int second1 = Convert.ToInt32(second);//将string类型的秒转换为int型的秒
string h = "";//定义小时为个位数的时候的补足位
string m = "";//定义分钟为个位数的时候的补足位
string s = "";//定义秒为个位数的时候的补足位
if (hour1 < 10)//当小时为个位数的时候
{
h = "0";//将小时补足位设为0
}
if (minute1 < 10)//当分钟为个位数的时候
{
m = "0";//将分钟补足位设为0
}
if (second1 < 10)//当秒为个位数的时候
{
s = "0";//将秒补足位设为0
}
string color1 = "green";//定义颜色属性为绿色
this.label1.ForeColor = System.Drawing.ColorTranslator.FromHtml(color1);//设置“时间定时”区域的label1的颜色为绿色
label1.Text = (h+hour + ":" + m + minute + ":" + s + second);//输出“时间定时”区域的时间,显示在label1上
C#将时间设定为两位数显示
最新推荐文章于 2023-07-05 14:09:23 发布