[ACCEPTED]-Remove leading zeros from time to show elapsed time-timespan

Accepted answer
Score: 17

Here's a one-liner (almost), assuming you 5 have the TimeSpan objectL

(new TimeSpan(0, 0, 30, 21, 3))
  .ToString(@"d\d\ hh\hmm\mss\s")
  .TrimStart(' ','d','h','m','s','0');

The sample code outputs

30m21s

The 4 first line just makes a TimeSpan object for the 3 sake of an example, .ToString formats it in the format 2 you're asking for and then .TrimStart removes the 1 leading characters you don't need.

Score: 12

Simple extension method should be enough:

static class Extensions
{ 
    public static string ToShortForm(this TimeSpan t)
    {
        string shortForm = "";
        if (t.Hours > 0)
        {
            shortForm += string.Format("{0}h", t.Hours.ToString());
        }
        if (t.Minutes > 0)
        {
            shortForm += string.Format("{0}m", t.Minutes.ToString());
        }
        if (t.Seconds > 0)
        {
            shortForm += string.Format("{0}s", t.Seconds.ToString());
        }
        return shortForm;
    } 
} 

Test 1 it with:

TimeSpan tsTest = new TimeSpan(10, 43, 27);
string output = tsTest.ToShortForm();
tsTest = new TimeSpan(0, 4, 12);
output = tsTest.ToShortForm();
tsTest = new TimeSpan(0, 0, 7);
output = tsTest.ToShortForm();
Score: 3

I don't think this can be done in a straightforward 3 way doing a custom format serializer - I'd 2 just roll my own:

TimeSpan delta = TimeSpan.Parse("09:03:07");
string displayTime = string.Empty;
if (delta.Hours > 0)
    displayTime += delta.Hours.ToString() + "h";

if (delta.Minutes > 0)
    displayTime += delta.Minutes.ToString() + "m";

if (delta.Seconds > 0)
    displayTime += delta.Seconds.ToString() + "s";

Note that this would only 1 work for positive time spans.

Score: 2

You can use string.Format to achieve this, along with 5 some conditional statements:

public static string GetSimplestTimeSpan(TimeSpan timeSpan)
{
    var result = string.Empty;
    if (timeSpan.Days > 0)
    {
        result += string.Format(
            @"{0:ddd\d}", timeSpan).TrimStart('0');
    }
    if (timeSpan.Hours > 0)
    {
        result += string.Format(
            @"{0:hh\h}", timeSpan).TrimStart('0');
    }
    if (timeSpan.Minutes > 0)
    {
        result += string.Format(
            @"{0:mm\m}", timeSpan).TrimStart('0');
    }
    if (timeSpan.Seconds > 0)
    {
        result += string.Format(
            @"{0:ss\s}", timeSpan).TrimStart('0');
    }
    return result;
}

Though, seeing 4 the answer by BrokenGlass I'm tempted to 3 say using Format here at all is overkill. However, it 2 does allow you to tweak the output of each 1 element of the elapsed time span if required.

Score: 1

Here's my take:

    Dim TimeTaken As String = TimeSpan.ToString("g") ' Supply TimeSpan
    If TimeTaken.Contains("0:00") Then
        TimeTaken = TimeTaken.Remove(0, 3)
    ElseIf TimeTaken.Contains("0:0") Then
        TimeTaken = TimeTaken.Remove(0, 2)
    End If

0

Score: 0
public static string ToFriendlyString(this TimeSpan timeSpan)
{
    string result = string.Empty;

    if (Math.Floor(timeSpan.TotalDays) > 0.0d)
        result += string.Format(@"{0:ddd}d ", timeSpan).TrimFirst('0');
    if (Math.Floor(timeSpan.TotalHours) > 0.0d)
        result += string.Format(@"{0:hh}h ", timeSpan).TrimFirst('0');
    if (Math.Floor(timeSpan.TotalMinutes) > 0.0d)
        result += string.Format(@"{0:mm}m ", timeSpan).TrimFirst('0');
    if (Math.Floor(timeSpan.TotalSeconds) > 0.0d)
        result += string.Format(@"{0:ss}s ", timeSpan).TrimFirst('0');
    else
        result += "0s";

    return result;
}

public static string TrimFirst(this string value, char c)
{
    if (value[0] == c)
        return value[1..];

    return value;
}

0

More Related questions