How To Convert Seconds Into Hh:mm:ss In C#? - QA With Experts
Có thể bạn quan tâm

- Ask a question
- Contribute an article
- Questions
- Articles
- Register
- Log in
- C#
- ASP.NET
- C/C++
- JAVA
- Javascript
- jQuery
- HTML
- CSS
- SQL
- Categories
- Tools
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 Answers
Answered by:- vikas_jkIf 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 answer
Answered by:- neenaYou 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 answerRelated 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 usTừ khóa » Hh Mm Ss C#
-
C# DateTime Format
-
C# DateTime To "YYYYMMDDHHMMSS" Format - Stack Overflow
-
Custom Date And Time Format Strings - Microsoft Docs
-
DateTime.ToString Method (System) - Microsoft Docs
-
How To Format The Hh:mm:ss Separators Of A TimeSpan | Phrase
-
DateTime Format In C# - C# Tutorial And Source Code
-
DateTime Format In C# - Code Maze
-
DateTime Formats In C# - TutorialsTeacher
-
C# ToString("hh:mm:ss") Code Example - Code Grepper
-
How To Convert A DateTime Object To A String In C#
-
DateTime Format: Hh:mm:ss Tt : Date Time Format
-
C# DateTime Format:Working With Date &Time Format In C#
-
Extracting Time From DateTime In Hh:mm:ss Format In C# - Forget Code
-
C# – String To Date Time “hh:mm:ss” Or “dd.mm.yyyy Hh:mm:ss” Format