Results 1 to 3 of 3

Thread: Precision of Elapsed Time using Time Span

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Location
    Sydney
    Posts
    51

    Precision of Elapsed Time using Time Span

    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.

    Code:
    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.

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Precision of Elapsed Time using Time Span

    you could do something like this
    Code:
    int t = 1;
    Console.WriteLine(t.ToString("00").Substring(0,2));
    t = 1234567;
    Console.WriteLine(t.ToString("00").Substring(0,2));

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Precision of Elapsed Time using Time Span

    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#.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width