9

I have a Timespan that I need to output in a particular format as shown below :-

TimeSpan TimeDifference = DateTime.Now - RandomDate;

I'm formatting the TimeSpan like this :-

string result = string.Format(@"{0:hh\:mm\:ss}", TimeDifference);

The Result will look something like this :-

"00:16:45.6184635"

How do I round those seconds to 0 decimal places?

Expected Result = 00:16:46

Thanks

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Derek
  • 8,300
  • 12
  • 56
  • 88

3 Answers3

11

Your code works with .NET 4 but not with 3.5 since there was a breaking change on 4, TimeSpan now implements IFormattable (see below).

What you can do on 3.5 or lower is, convert the TimeSpan to DateTime and use ToString:

DateTime dtime = DateTime.MinValue.Add(TimeDifference);
string result = dtime.ToString(@"hh\:mm\:ss");

Here you can see the non-working + working version:http://ideone.com/Ak1HuD


Edit I think the reason why it works sometimes and sometimes not is that since .NET 4.0 TimeSpan implements IFormattable which seem to be used by String.Format.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks Tim, appreciate it. – Derek Jan 31 '13 at 09:34
  • It's weird, I think the issue must come with the DateTime that I am deducting from DateTime.Now in my app. As in exaples above, my code does work. – Derek Jan 31 '13 at 09:43
  • @Derek: Edited my answer to provide a way on 3.5 by converting the `TimeSpan` to `DateTime`. The examples work because they simply add whole hours, then you have no rounding issue with fractional seconds. – Tim Schmelter Jan 31 '13 at 09:49
  • +1 Tim, nice explanation, `TimeSpan.ToString` doesn't have an overload for string format in .Net 2.0. – Habib Jan 31 '13 at 10:01
7

Your code should work fine (after removing minor syntax errors). Consider the following example:

TimeSpan TimeDifference = DateTime.Now - DateTime.Now.AddHours(-6);
string result = string.Format(@"{0:hh\:mm\:ss}", TimeDifference);
Console.WriteLine("TimeSpan: {0}", TimeDifference.ToString());
Console.WriteLine("Formatted TimeSpan: {0}", result);

Output:

TimeSpan: 05:59:59.9990235
Formatted TimeSpan: 05:59:59
Habib
  • 219,104
  • 29
  • 407
  • 436
5

Works fine for me.

For example, this program:

using System;

namespace Demo
{
    public static class Program
    {
        private static void Main(string[] args)
        {
            DateTime then = new DateTime(2013, 1, 30, 0, 1, 3);
            TimeSpan ts = DateTime.Now - then;

            Console.WriteLine(ts.ToString());
            Console.WriteLine(ts.ToString(@"hh\:mm\:ss"));
            Console.WriteLine(string.Format(@"{0:hh\:mm\:ss}", ts));

            // Or, with rounding:
            TimeSpan rounded = TimeSpan.FromSeconds((int)(0.5 + ts.TotalSeconds));
            Console.WriteLine(rounded.ToString(@"hh\:mm\:ss"));
        }
    }
}

Outputs something like:

1.09:20:22.5070754
09:20:22
09:20:22
09:20:23 <- Note rounded up to :23
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276