-3

I want to implement the most flexible string format for a TimeSpan object. Suppose you've this code:

string format = ".................";
TimeSpan ts = TimeSpan.FromSeconds(7200);
Debug.WriteLine(ts.ToString(format));

I want to specify a parameter (in app.config of my console application?) that contains a string format to switch different output format without touching the C# code. For example: 7200sec (outputs only the total seconds) 2h 00min 00sec (outputs the TimeSpan in a friendly manner) 120 min (outputs only the total minutes) and so on.

I don't want to adapt my code for each case.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Igor Damiani
  • 1,897
  • 9
  • 12
  • 3
    And your question is? The [documentation on what's possible with `TimeSpan` formats](https://learn.microsoft.com/dotnet/standard/base-types/custom-timespan-format-strings) is pretty straightforward. – Jeroen Mostert Oct 08 '20 at 11:10
  • 1
    You can't do this with a format string. Are you looking for something like ["humanizer"](https://github.com/Humanizr/Humanizer#humanize-timespan), which figures out the "most readable" format to display things in? – CodeCaster Oct 08 '20 at 11:13
  • 1
    `TimeSpan.ToString()` uses the DateTimeFormatInfo of the current culture. That's probably the only case where it makes sense to use an ambient timespan format property. Even on the same form, different fields will require different formats for eg a duration in seconds or time of day. It's possible to create a custom CultureInfo, but that would affect *every* DateTime or Timespan serialization – Panagiotis Kanavos Oct 08 '20 at 11:16

1 Answers1

-1

Mix of TimeSpan.ToString ans string.format should be useful.

string format = "{2}h {0:%mm}min {0:%ss}sec";
string format2 = "{4}min";
TimeSpan ts = TimeSpan.FromSeconds(7200);
Console.WriteLine(string.Format(format, ts, ts.TotalDays, ts.TotalHours, ts.TotalMinutes, ts.TotalSeconds));
Console.WriteLine(string.Format(format2, ts, ts.TotalDays, ts.TotalHours, ts.TotalMinutes, ts.TotalSeconds));
Mustafa Arslan
  • 774
  • 5
  • 13
  • And how does your code determine (at runtime) which format string to use? – CodeCaster Oct 08 '20 at 11:34
  • And you are proposing two different format strings. They appear to want to handle everything between "2 seconds" and "10 years, 5 months, 2 days and 1 minute". How many different format strings do you need for that, and again, how will the code determine which one to use? – CodeCaster Oct 08 '20 at 11:37
  • I understand that Igor want to define a format string on config file. If he changes this string, he don't want to change code. If i misunderstand, of course you are right. – Mustafa Arslan Oct 08 '20 at 11:40
  • Yes, and their "format string in a config file" is exactly the part that isn't going to work. There is no one format string to do all of this. They do not know the value of 7200 at compile time, so they need to determine at runtime which format string to use. Your answer does not solve that problem. – CodeCaster Oct 08 '20 at 11:45