PDA

View Full Version : Time format


Mindset
01-29-2009, 11:00 AM
I am trying to get just the time portion of a ToTime[1] call.

I have been at it all afternoon.

I want it to look like "01:03" but I get 2 difficulties.

1.The only way I have been half successful is to use stringbuilder and then chop up the above into two segments. Is there an easier way?

2. 0103 comes out as 10:03

edit - the ToTime seems to throw away the zeros? eg 00:51 becomes 51?

string mystr = (ToTime(Time[1])).ToString();
StringBuilder leftbit = new StringBuilder();
leftbit.Append(mystr.Substring(0,2)+":");
leftbit.Append(mystr.Substring(2,2));
Print("My time" + leftbit.ToString());

Ralph
01-29-2009, 12:48 PM
Without having tested, I think it is something like: Time[1].ToString("t").

Mindset
01-29-2009, 01:12 PM
That is just so frustrating - I looked through the format protocols. Guess I must have just missed it.
thanks Ralph.

NinjaTrader_Josh
01-29-2009, 01:20 PM
ToTime() formats the time into an integer.

00:51 will show up as 51.
11:51 will show up as 1151.

Building your own string would be the way I would proceed. You can also access the hours and minutes directly from the DateTime object.

Time[0].Hour
Time[0].Minute

Mindset
02-02-2009, 12:51 AM
Would it be proper to use Time[0].Hour + ":" + Time[0].Minute or
build it with string builder?
I seem to remember reading that concatenation is relatively resource / memory intensive.

Edit - actually you can do it all in Time[1].ToString("t") which seems the neatest way to achieve this. No stringbuilder required.

NinjaTrader_Josh
02-02-2009, 07:20 AM
Glad you got it resolved.

Ralph
02-02-2009, 07:45 AM
Agree with your conclusion, Mindset. Long time ago I tried to outperform the standard-C string-functions by self written code with pointers. But I never reached the same performance. Since this time I first look what functions are offered by a language before coding my own stuff.

Regards
Ralph