Get Date Time Hours and Minutes with leading Zero

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
tt - AM / PM 

There are many more. You should check the MSDN documentation for the full reference: https://msdn.microsoft.com/library/zdtaw1bw.aspx


If you only want the hours and minutes as separated values you can use

DateTime.Now.Hour.ToString("00.##") + ":" + DateTime.Now.Minute.ToString("00.##");

You should check out MSDN's Standard Date And Time Format Strings

Tags:

C#