

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?
DateTime
DateTime is a structure of value Type like int, double etc. It is available in System namespace and present in mscorlib.dll assembly.It implements interfaces like IComparable, IFormattable, IConvertible, ISerializable, IComparable, IEquatable.DateTime contains properties like Day, Month, Year, Hour, Minute, Second, DayOfWeek and others in a DateTime object.
TimeSpan
TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds.TimeSpan is used to compare two DateTime objects to find the difference between two dates. TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds methods to create TimeSpan objects from days, hours, minutes, seconds, and milliseconds respectively.
Example 1
static void Main(string[] args){ TimeSpan t = TimeSpan.FromSeconds(3752); string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); System.Console.WriteLine(answer); Console.ReadLine(); }
Output
01h:02m:32s:000ms
Example 2
static void Main(string[] args){ TimeSpan t = TimeSpan.FromSeconds(6); string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); System.Console.WriteLine(answer); Console.ReadLine(); }
Output
00h:00m:06s:000ms
- Related Questions & Answers
- Python program to convert seconds into hours, minutes and seconds
- How to convert JavaScript seconds to minutes and seconds?
- Converting seconds into days, hours, minutes and seconds in C++
- How to get hour, minutes and seconds in android using offset time API class?
- C++ Program for converting hours into minutes and seconds
- Remove Seconds/ Milliseconds from Date and convert to ISO String?
- How to sum time in MySQL by converting into seconds?
- How to convert time seconds to h:m:s format in Python?
- Program to convert hour minutes’ time to text format in Python
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- Convert HH:MM:SS to seconds with JavaScript?
- How to get min, seconds and milliseconds from datetime.now() in Python?
- Hours and minutes from number of seconds using JavaScript
- MySQL query to convert timediff() to seconds?
- Converting seconds in years days hours and minutes in JavaScript