How To Convert Seconds Into Hh:mm:ss In C#? - QA With Experts

  • Ask a question
  • Contribute an article
  • Questions
  • Articles
  • Register
  • Log in
  • C#
  • ASP.NET
  • C/C++
  • JAVA
  • Javascript
  • jQuery
  • HTML
  • CSS
  • SQL
  • Categories
  •  
  •  
  •  
How to convert seconds into hh:mm:ss in C#?

I am trying to work on Audio file and have seconds as int in C#, but I would like to convert seconds into hh:mm:ss ( hours, minutes and seconds) format, so How can I do it easily in C#?

For example: 20 Seconds = 00:00:20 in hour,minutes,seconds format.

Asked by:- bhanu0 : 8621 At:- 4/1/2021 1:56:02 PMC# datetime format Commentadd comment to question2 AnswersprofileImage Answered by:- vikas_jk

If you are using .NET 4.0 or above, you can simply convert seconds into TimeSpan and then get hours:minutes:secondsTimeSpan time = TimeSpan.FromSeconds(seconds); // backslash is used to ":" colon formatting you will not see it in output string str = time.ToString(@"hh\:mm\:ss");

Here is the sample Programusing System; public class Program { public static void Main() { TimeSpan time = TimeSpan.FromSeconds(890); // backslash is used to ":" colon formatting you will not see it in output string str = time .ToString(@"hh\:mm\:ss"); Console.WriteLine(str); } }

Output:00:14:50

You can try it on .NET fiddle: https://dotnetfiddle.net/mMsjxH

If you want to add "miliseconds" also, then use below formatstring str = time.ToString(@"hh\:mm\:ss\:fff");2 At:- 4/1/2021 2:13:42 PM Excellent, thanks for quick answer, fiddle helps. 0 By : bhanu - at :- 4/1/2021 2:14:58 PM Comment comment to above answerprofileImage Answered by:- neena

You can also simply use TimeSpan.FromSeconds(90) where 90 = total number of seconds, this code will convert seconds into hh:mm:ss

Here is the Sample C# Codeusing System; public class Program { public static void Main() { Console.WriteLine(TimeSpan.FromSeconds(90)); } }

Output:00:01:30

Hope it helps, thanks.0 At:- 11/25/2021 3:23:10 PM Comment comment to above answerCoffee iconBuy us a coffee Patreon Become a Patron Login/Register to answerOrRegister directly by posting answer/detailsFull Name *Email * By posting your answer you agree on privacy policy & terms of use

Related Articles
6448Transfer e-mails from one exchange server to another using EWS Api in C# .NET19900Create XML sitemap dynamically in ASP.NET MVC C#5719Abstract class in C#10303Sealed Class in C# (Explanation with example)10028Interface in C# (With Example)

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly

Related Questions
27884400. That’s an error. Error: redirect_uri_mismatch - Google OAuth Authentication11099how to generate dynamic url using .NET MVC8044How to convert JSON String into C# class object15592Cannot convert null to a value type JSON error9850DbArithmeticExpression arguments must have a numeric common typeFollow us

Từ khóa » Hh Mm Ss C#