Click here to Skip to main content
15,885,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I need the current time of some tests so I was using
VB
Time = DateAndTime.Now.ToString
TimeOnly = Time.Substring(11, 5)
To get the time only as I don't need the date in this part, this works fine here but on the customer system it appears that 10:09 becomes 0:09 does DateAndTime works from the PC Windows time, doesn't it(?), could I be trimming too early in the substring ?
Glenn
Posted
Comments
Sergey Alexandrovich Kryukov 28-Apr-14 11:55am    
What's wrong with reading standard MSDN documentation?
—SA

I would not do string operations but something like this:

var result = string.Format("{0}:{1}", DateTime.Now.Hour, DateTime.Now.Minute);
 
Share this answer
 
Comments
glennPattonWork3 28-Apr-14 11:52am    
Thanks I will give it a go!
Sergey Alexandrovich Kryukov 28-Apr-14 13:12pm    
Yes, something like this; a 5.
—SA
You don't need to trim anything. You just need to work with data itself, not string representing data.
Please see, look at the properties: http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

This is where you can extract time only.

And you can control the formatting of time as string the way you want. Look at all System.DateTime.ToString methods. The format can be custom, depend on culture or system settings, and so on.

See also format specifiers:
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Comments
Manas Bhardwaj 28-Apr-14 11:58am    
Right. MSDN documentation should be the first place before asking question anywhere. +5!
Sergey Alexandrovich Kryukov 28-Apr-14 13:11pm    
Thank you, Manas.
—SA

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