title: Calculating average time using TimeSpans in C#
body: <p>I am trying to calculate an average time by subtracting end time by start time then dividing the difference by a count. In my case the count is products scanned. I want to output the result in a string using this format "HH:mm:ss". I have been playing around with my code but I can't get it to work. </p>
  
  <p>Here are my variables:</p>
  
  <pre><code>    private DateTime woStartTime;
      private DateTime woEndTime;
      private TimeSpan difference;
      private long average;
  </code></pre>
  
  <p>Here is the value for Start Time</p>
  
  <pre><code>    woStartTime = DateTime.Now;
  </code></pre>
  
  <p>Here is my method for calculating average time</p>
  
  <pre><code> public void Calculate_Average_Time()
      {
          try
          {
              woEndTime = DateTime.Now;
              difference = woEndTime - woStartTime;
              long dTime = Convert.ToInt64(difference.Ticks);
              average = dTime / Scanned;
              DateTime aTimeS = Convert.ToDateTime((average / 86400M).ToString("HH:mm:ss", CultureInfo.InvariantCulture));
              lblError.Text = aTimeS.ToString(); // display to verify results
          }
          catch(Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
      }
  </code></pre>
  
  <p>I keep getting an exception error: string was not recognized as a valid DateTime. There is an unknown word starting at index 0.</p>
  
  <p>What am I doing wrong? Please help.</p>