display timespan nicely
Asked Answered
O

4

16

Excuse the rough code, I'm trying to display the duration of videos given the time in seconds. I've had a go below but it's not working properly.

I want it to just display nicely - i.e should display 9m:59s not 09m:59s.

If hours are zero dont display hours, if minutes are zero dont display minutes.

public static string GetTimeSpan(int secs)
{
    TimeSpan t = TimeSpan.FromSeconds(secs);

    string answer;
    if (secs < 60)
    {
        answer = string.Format("{0:D2}s", t.Seconds);
    }
    else if (secs < 600)//tenmins
    {
        answer = string.Format("{0:m}m:{1:D2}s", t.Minutes, t.Seconds);

    }
    else if (secs < 3600)//hour
    {
        answer = string.Format("{0:mm}m:{1:D2}s", t.Minutes, t.Seconds);
    }
    else
    {
        answer = string.Format("{0:h}h:{1:D2}m:{2:D2}s",
                                    t.Hours,
                                    t.Minutes,
                                    t.Seconds);
    }

    return answer;
}
Olympe answered 15/2, 2012 at 10:55 Comment(3)
so what is your question? or what do you expect to happen and what does happen? please do not expect me to guess :)Lorrainelorrayne
the question is display time duration nicely given the seconds. if hours are zero dont display hours, if minutes are zero dont display minutes, also would want in format 9m:59s not 09m:59s for single figure minutes.Olympe
Your format strings are wrong, there is no "m" or "h" specifier.See MSDN. You are just dealing with "numbers" not "minutes/seconds"! Also you do not need to explicitly specify "2 places" for values > 10.Monocotyledon
C
28

Something like:

public static string PrintTimeSpan(int secs)
{
   TimeSpan t = TimeSpan.FromSeconds(secs);
   string answer;
   if (t.TotalMinutes < 1.0)
   {
     answer = String.Format("{0}s", t.Seconds);
   }
   else if (t.TotalHours < 1.0)
   {
     answer = String.Format("{0}m:{1:D2}s", t.Minutes, t.Seconds);
   }
   else // more than 1 hour
   {
     answer = String.Format("{0}h:{1:D2}m:{2:D2}s", (int)t.TotalHours, t.Minutes, t.Seconds);
   }

   return answer;
}
Construct answered 15/2, 2012 at 11:8 Comment(2)
Why use t.TotalHours when you can use t.Hours?Argybargy
@FandiSusanto If the timespan is "1 day and 2 hours", TotalHours will return 26, Hours will return 2.Monocotyledon
T
3

I think you can simplify this by removing the "D2" aspect of the format and then you won't need a special case for the under ten minutes option. Basically just using

string.Format("{0}m:{1}s", t.Minutes, t.Seconds);

will get you one or two digits as required. So your final case is:

string.Format("{0}h:{1}m:{2}s", t.Hours, t.Minutes, t.Seconds);
Touched answered 15/2, 2012 at 11:16 Comment(0)
C
3
readonly static Char[] _colon_zero = { ':', '0' };
// ...

var ts = new TimeSpan(DateTime.Now.Ticks);
String s = ts.ToString("h\\:mm\\:ss\\.ffff").TrimStart(_colon_zero);
.0321
6.0159
19.4833
8:22.0010
1:04:2394
19:54:03.4883
Cockcrow answered 4/7, 2017 at 18:21 Comment(0)
S
2

According to msdn try this:

if (secs < 60)
{
    answer = t.Format("s");
}
else if (secs < 600)//tenmins
{
    answer = t.Format("m:s");
}
// ...
Sikora answered 15/2, 2012 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.