将分钟转换为两位数

Moh*_*ava 3 c# datetime

请看下面的代码:

DateTime timerTest = timerView;
txbTimer.Text = timerTest.Day + "/" + timerTest.Month + "/" + timerTest.Year + " " + timerTest.Hour + ":" +  timerTest.Minute;
Run Code Online (Sandbox Code Playgroud)

我想在我得到的日期和时间中得到日期和时间txbTimer.如果日期是14/8/2015 14:10

但是当时间(分钟)是01 - 09时,txbTimer将成为2015年8月14日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();
Run Code Online (Sandbox Code Playgroud)

Hos*_*Rad 6

用这个

timerTest.Minute.ToString("D2");
Run Code Online (Sandbox Code Playgroud)

有关更多格式,请点击此链接

这是MSDN提供的示例

1234.ToString("D")     -> 1234
(-1234).ToString("D6") -> -001234
Run Code Online (Sandbox Code Playgroud)

  • 您选择了此答案,但是在可以(并且应该)使用日期时间格式时不要使用数字格式。只是说.. (3认同)

NAS*_*SER 5

DateTime timerTest = timerView;
var result = timerTest.ToString("dd/M/yyyy hh:mm");
/*
dd -> two digit day
M -> one digit month
yyyy -> four digit year
hh -> two digit hour
mm -> two digit minute
*/
Run Code Online (Sandbox Code Playgroud)

HH:(00-23)看起来像00,01..23.
hh :(上午01点到下午12点)看起来像01,02..12.