获取日期时间小时和分钟与领先的零 [英] Get Date Time Hours and Minutes with leading Zero
问题描述
我试图找出最简单的代码就从DateTime.Now功能小时和分钟添加前导零。我只需要小时和分钟结合起来,我不需要日期的其余部分。
最新最好的方式做到这一点?
我的代码如下所示:
DateTime.Now.Hour.ToString()+ :+ DateTime.Now.Minute.ToString();
不过,我得到这样的16个数据:4 16:04,我需要它看起来像16 :04。我熟悉日期时间格式的MSDN文章,但我没有看到任何专门解决这个。
是否有可能结合'DateTime.Now.Hour.ToString ()+:+ DateTime.Now.Minute.ToString();
如果不是我会拉只有HH:MM出DateTime.Now的容易吗?
寻找代码可能至少行,因为这是我将经常使用的东西。
谢谢
DateTime.Now.ToString(HH:MM )//非军用时间
DateTime.Now.ToString(HH:MM)//军事时间(24小时制)
使用 HH
VS ^ h
将做一个0领先与同毫米
几分钟。如果你想秒,可以使用 SS
MM - 月与领先0
M - 月,没有前导0
DD - 日与领先的0
D - 一天没有前导0
YYYY - 4位数的年份
YY - 2位数的年份
HH - 军事小时与领先的0
H - 军事小时,没有前导0
HH - 小时与领先的0
H - 小时,没有前导0
毫米 - 与领先的分0
M - 分钟无前导0
SS - 第二与领先的0
S - 第二,没有前导0
有许多。您应该检查MSDN文档完整的参考:
https://msdn.microsoft.com/library/zdtaw1bw的.aspx
I'm trying to figure out the simplest code to add leading zeros on to the hours and minutes from the DateTime.Now function. I need to combine only the hours and minutes, and I don't need the rest of the date.
Whats the best way to do this?
My code looks like this:
DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
However I get data such as 16:4 for 4:04PM and I need it to look like 16:04. I'm familiar with the msdn articles on datetime formatting but I didn't see anything that addresses this specifically.
Is it possible combining `DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
If not how would I pull only the HH:MM out of DateTime.Now easily?
Looking for least lines of code possible because this is something that I will be utilizing often. Thanks
DateTime.Now.ToString("hh:mm") // for non military time
DateTime.Now.ToString("HH:mm") // for military time (24 hour clock)
Using hh
vs h
will do a leading 0. Same with mm
for minutes. If you want seconds, you can use ss
.
MM - Month with leading 0
M - Month without leading 0
dd - Day with leading 0
d - Day without leading 0
yyyy - 4 Digit year
yy - 2 Digit year
HH - Military Hour with leading 0
H - Military Hour without leading 0
hh - Hour with leading 0
h - Hour without leading 0
mm - Minute with leading 0
m - Minute without leading 0
ss - Second with leading 0
s - Second without leading 0
There are many more. You should check the MSDN documentation for the full reference: https://msdn.microsoft.com/library/zdtaw1bw.aspx
这篇关于获取日期时间小时和分钟与领先的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!