Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:07:18+00:00 2026-05-30T04:07:18+00:00

Excuse the rough code, I’m trying to display the duration of videos given the

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:07:20+00:00Added an answer on May 30, 2026 at 4:07 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Excuse my ignorance but I'm having a tough time figuring this out. I'm trying
Excuse the beginner level of this question. I have the following simple code, but
Excuse my ignorance, but I've not done an Xcode upgrade before. I thought I'd
Excuse me for not quite a programming question, but I need to burn a
Excuse me if this post is not related to this forum, but I couldn't
please excuse the Rail noob question. I'm trying to display a list of associated
SOLVED! My Activity Stack looks like this, excuse the rough diagram! A-->B-->C '-->D If
Excuse the n00bness of this question, but I have a web application where I
Excuse the poor phrasing, I know it's possible but I can't figure out what
Excuse the 'noob' question, but can someone please explain the way ASP.NET's buttons work.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.