0

I'm doing the follwing:

//TimeSpan rebateTime
//int percentage
string text = string.Format(
    CultureInfo.CurrentCulture, 
    "Rebate {0}% during {1:hh} h {1:mm} min", 
    percentage, 
    rebateTime;

On my dev pc text contains:

Rebate 32% during 05 h 00 min

On my dev test server text contains:

Rebate 32% during 05 h 00 min

On my the shared test server text contains:

Rebate 32% during 05:00:00 h 05:00:00 min

How can this be possible at all?

Carl R
  • 8,104
  • 5
  • 48
  • 80
  • 2
    The code you've posted isn't valid... it's not even valid C#, and you've only specified two of the three format arguments. Please post a short but complete program that demonstrates the problem. – Jon Skeet Feb 12 '14 at 11:06
  • 4
    Also, please tell us what version of .NET you're using on the server. I believe custom formatting for TimeSpan was only introduced in .NET 4. – Jon Skeet Feb 12 '14 at 11:08
  • @JonSkeet Great tip! It sounds like it's the issue. I'll verify. – Carl R Feb 12 '14 at 11:34
  • @JonSkeet The code is updated, it uses the same argument for both hour and minute. – Carl R Feb 12 '14 at 11:36
  • 2
    It's still not valid code, and it's still not a short but complete example. But hopefully this is just a matter of the version of .NET that you're using. (It doesn't help that we have no idea whether this is an ASP.NET site, a WCF service etc.) – Jon Skeet Feb 12 '14 at 11:59
  • @CarlR the code is not valid because it's missing closing brackets ")" between "rebateTime" and ";" – Andre Figueiredo Feb 12 '14 at 13:42
  • Skeet is right. In .NET 3.5 and earlier, the `TimeSpan` struct did not implement `IFormattable`. In that case `string.Format` method will ignore the formats (here `hh` and `mm`) entirely. It has nowhere to pass them. Since .NET 4.0 (2010), a `TimeSpan` is `IFormattable`, and so the implementation of [`IFormattable.ToString` method](http://msdn.microsoft.com/en-us/library/ee436754.aspx) is used. – Jeppe Stig Nielsen Feb 12 '14 at 14:02

3 Answers3

0

I reproduced you error here.

This format only works from .NET 4. Probably .NET 4 isn't installed in your shared server.

Look at section Formatting a TimeSpan Value in TimeSpan Structure MSDN docs.

To check this, create a simple console project:

    static void Main(string[] args)
    {
        Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "Rebate {0}% during {1:hh} h {1:mm} min", 0.15, new TimeSpan(DateTime.Now.Ticks)));
        Console.ReadKey();
    }

Outputs:

.NET 4

"Rebate 0.15% during 11 h e 47 min"

.NET 3.5

"Rebate 0,15% during 735275.11:48:48.7362198 h 735275.11:48:48.7362198 min"
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
0

As others have pointed out, TimeSpan didn't have custom formating before .NET 4. However, the alternate solution isn't complicated at all:

string.Format
(
    "Rebate {0}% during {1:##} h {2:##} min", 
    percentage, 
    rebateTime.TotalHours, 
    rebateTime.Minutes
);
Luaan
  • 62,244
  • 7
  • 97
  • 116
0

As it happens I had the exact same issue today. I fixed it by using an extension method:

    public static string ToReadableString(this TimeSpan span)
    {
        string formatted = string.Format("{0}{1}{2}{3}",
            span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Hours > 0 ? string.Format("{0:00} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Minutes > 0 ? string.Format("{0:00} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Seconds > 0 ? string.Format("{0:00} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);

        if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

        if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";

        return formatted;
    }

When it's used on a timespan you will get the time in the following format (x days x hours x minutes x seconds) whenever something isn't available it will be hidden.

woutervs
  • 1,500
  • 12
  • 28
  • The reason it happened was indeed because I too was using .NET 3.5 framework (as it was for a sharepoint 2009 migration project). – woutervs Feb 12 '14 at 14:18