PDA

Click to See Complete Forum and Search --> : Precision of Elapsed Time using Time Span


cybertechno
May 25th, 2005, 06:51 PM
In my application Iam using a label that shows the elapsed time.
Iam using TimeSpan Class.The TimeSpan has several constructors and Iam using the long constructor.

ts = new TimeSpan((long) (ts.Ticks*(((double) iFilesTotal/(double) iFilesProcessed) - 1))); lblTimeRemaining.Text = ts.ToString();

The result is this "00:12:23:59:6754346"
Days:Hrs:Mins:Sec:milliseconds
I dont need the milliseconds for 7 decimal places, and only 2.

What is the solution?
Can I use a String Format?If yes how? I have the following VB code but what would be the C# equivalent?
String.Format("{0}:{1}:{2}:{3}.{4}", _
ts.Days.ToString().PadLeft(2, "0"c), _
ts.Hours.ToString().PadLeft(2, "0"c), _
ts.Minutes.ToString().PadLeft(2, "0"c), _
ts.Seconds.ToString().PadLeft(2, "0"c), _
ts.Milliseconds.ToString().PadLeft(3,"0"c).Substring(0, 2))


Please help.

DeadEyes
May 26th, 2005, 03:42 AM
you could do something like this

int t = 1;
Console.WriteLine(t.ToString("00").Substring(0,2));
t = 1234567;
Console.WriteLine(t.ToString("00").Substring(0,2));

jmcilhinney
May 26th, 2005, 04:46 AM
Guess what... I work in C# too! The only difference between the VB.NET code and the C# is that in VB a char looks like a one-character string followed by a lower case C, e.g. the character for zero is "0"c. In C# the same character is '0'. The syntax for VB.NET and C# is very similar in most cases and exactly the same in many, given that all the .NET framework objects are (mostly) the same. Had you just started typing in C#, IntelliSense would have told you what you needed to know. The only other thing is that you'll need to remove the line-continuation characters (the underscores) for C#.