请看下面的代码:
DateTime timerTest = timerView;
txbTimer.Text = timerTest.Day + "/" + timerTest.Month + "/" + timerTest.Year + " " + timerTest.Hour + ":" + timerTest.Minute;
我想在我得到的 txbTimer
中得到日期和时间。如果日期是 14/8/2015 14:10
但是当时间(分钟)为 01 - 09 时,txbTimer
将变为 14/8/2015 14:1 在这种情况下我想要预期的输出,如 14:01 而不是 14:1
请帮帮我
编辑
我的代码是
我认为增加小时数和天数的行可能会发生变化,所以这里是更新后的代码。
DateTime timerView = DateTime.Now;
DateTime timerTest = timerView;
if(robHour.Checked == true)
timerTest = timerView.Add(new TimeSpan(0, 1, 0, 0));
else if(robDay.Checked == true)
timerTest = timerView.Add(new TimeSpan(1, 0, 0, 0));
txbTimer.Text = timerTest.ToString(); //timerTest.Day + "/" + timerTest.Month + "/" + timerTest.Year + " " + timerTest.Hour + ":" + timerTest.Minute.ToString();
最佳答案
使用这个
timerTest.Minute.ToString("D2");
有关更多格式,请遵循此 link
这里是MSDN提供的例子
1234.ToString("D") -> 1234 (-1234).ToString("D6") -> -001234
关于c# - 将分钟转换为两位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32003792/