c# - 将分钟转换为两位数

标签 c# datetime

请看下面的代码:

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/

相关文章:

postgresql - 检查 postgresql 中时态数据库的最小可变性

c# - 使用来自另一个列表的参数对一个列表进行排序,并使用可连接的参数

c# - 将 String 写入 Stream 并将其读回不起作用

c# - moq 模拟 buffer[] 参数

c# - Visual C# 集合 "map"函数?

PHP 选择日期等于日期时间的位置

Javascript 日期问题,时区不正确

c# - 索引超出范围。必须为非负数且小于集合的大小。参数名称 : index

c# - linq中的日期时间

c# - 如何获取不同时区的当前 UTC 偏移量?