display timespan nicely

前端 未结 4 2241
心在旅途
心在旅途 2021-02-19 01:48

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 d

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-19 02:31

    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;
    }
    

提交回复
热议问题