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.

–

6 Answers

6

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.

–

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 minimum 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 MSDN since the methods in Mono and in .NET are the same. The implementation might be a bit different, but they are compatible and implement the same specs.

–

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.

Ive never tried this but I would be tempted to inject the legacy animation in to the OnAnimator callbacks or in LateUpdate (which should be kept minimal).

–

System.DateTime.Now.ToString(“yyyy/MM/dd hh:mm:ss”);

Works! How do you show am/pm?

–

Found it. "tt" returns am/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

–

Had the same problem made a video that simplifies it and gives you a link to string codes so the time can be displayed anyway you want. How to format time in unity (Unity Tutorial) - YouTube