Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Panic has ensued while I have had 2 days off, Anyway I am doing
VB
TimeOnly = String.Format("{0}:{1}", DateTime.Now.Hour, DateAndTime.Now.Minute.ToString)


This gives the time reported by Windows i.e. 14:25 but when the time is say 09:56 like it is now it returns 9:56 I need a method of re formatting the time so it has a leading 0 so 9:56
becomes 09:56 I have done similar in the past in C# with
DateTime.Now.ToString("hh") or DateTime.Now.ToString("HH")

VB
TimeOnly = String.Format("{hh}:{mm}", DateTime.Now.Hour, DateAndTime.Now.Minute.ToString)

gives: "time is hh:mm" the same if DateTime.Now.Hour.ToString(hh) is not declared.
There has to be a more elegant solution than
VB
if (DateAndTime.Now.Hour < 10) Then 
doesn't there?
Posted

What about this?

string timeOnly = DateTime.Now.ToString("hh:mm");
 
Share this answer
 
Comments
glennPattonWork3 6-May-14 5:19am    
Seems to work, will that get rid of the Date stuff?
Manas Bhardwaj 6-May-14 5:46am    
Yes!
glennPattonWork3 6-May-14 5:48am    
Cool, have a 5!
Manas Bhardwaj 6-May-14 5:59am    
thx!
What's wrong with
VB
TimeOnly = DateTime.Now.ToString("hh:mm")

?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900