- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- What is the best way to read an entire file into a std::string in C++?
- What is the best way to convert a number to a string in JavaScript?
- Best way to store date/time in MongoDB?
- What is the best way to iterate over a Dictionary in C#?
- Python program to convert seconds into hours, minutes and seconds
- Convert 40 minutes into seconds.
- What is the best way to check capacity in Java?
- What is the best way to concatenate strings in JavaScript?
- What is the easiest way to convert int to string in C++
- What is the best way to stop Brain Drain?
- What is the best way to earn money online?
- Convert given time into words in C++
- Can anyone suggest how to pass time the best possible way?
- What is the best way to compare two strings in JavaScript?
- What is the best way to add an event in JavaScript?
