- Home /
How do I format a string into days:minutes:hours:seconds?
I know that I need to use String.Format but there is very little explanation of its usage in the mono docs. So far I have..
timeText = String.Format ("{0:00}:{0:00}:{1:00}:{0:00}",displayDays,displayHours, displayMinutes, displaySeconds);
..which does nothing as I have no idea what the 1's and 0's represent. Can anyone shed some light on this?
the numbers before the colon represent the index of the objects after the formatting strin. SO in your example, 0 is displayDays, 1 is displayHours and 2 would be displaySeconds.
Answer by robhuhn · Jun 21, 2012 at 12:53 PM
I prefer using TimeSpan (namespace System) in c#. It's short and simple:
TimeSpan timeSpan = TimeSpan.FromSeconds(time);
string timeText = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
Just a note, TimeSpan is in the System namespace, so you need to add "using System;" to your header.
This answer is awful. You do not explain what the heck {0:d2} means. It may be "short and simple" when you understand it, but looking at {0:d2} really makes no sense by itself. Telling people it's easy doesn't actually help them understand.
Answer by Grimmy · Aug 22, 2010 at 10:06 AM
Well I figured out that the first zero is the order of the string array..ie it should read
currentCaseTimeText = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}",displayDays,displayHours, displayMinutes, displaySeconds);
And that gives me 00:00:00:00 with the correct vars assigned to the correctly formatted areas.
Right, the first zero is the order of the variable to show. The 00 means the $$anonymous$$imum number of digits to use for displaying that value. Two zeros tell the parser to display the single-digit values (1, 2, 3...) with a leading 0: 01,02,03. Using 000 would lead to 001, 002, 003.
Also, if your values are float, you can specify the number of decimal digits to use by adding a dot and the desired number of decimals as zeros: {0:00.000} --> 03.234
You can also use the String.padLeft() and String.PadRight() methods. strBlah.ToString().PadLeft(2, '0'); Plus, if you want to search for info - you can use $$anonymous$$SDN since the methods in $$anonymous$$ono and in .NET are the same. The implementation might be a bit different, but they are compatible and implement the same specs.
Answer by yoyo · Dec 03, 2010 at 05:58 PM
Here's the bit of code I'm using to display floating point seconds as MM:SS.FF ...
string FormatSeconds(float elapsed)
{
int d = (int)(elapsed * 100.0f);
int minutes = d / (60 * 100);
int seconds = (d % (60 * 100)) / 100;
int hundredths = d % 100;
return String.Format("{0:00}:{1:00}.{2:00}", minutes, seconds, hundredths);
}
(EDIT) Recently realized I can simply use System.DateTime, as demonstrated in this javascript fragment:
var date : System.DateTime;
date = new System.DateTime(seconds * System.TimeSpan.TicksPerSecond);
If you want it as a nicely formatted string, use date.ToString(). This can also take a format parameter to control the formatting details, see MSDN docs for more info.
Answer by yoyo · Dec 03, 2010 at 05:50 PM
If you have a DateTime you can use custom format strings as explained here ... http://social.msdn.microsoft.com/Search/en-us?query=custom+datetime+format+strings
The first result from this search helped me: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings