- Home /
how to Format time float (days/hours/minutes/seconds)
I just can't figure it out following
I've done code:
if (TimeToDisableHelp == 0 ){
GUILayout.Label("Time Help to Fade is false");
}
else if (TimeToDisableHelp < 60){
GUILayout.Label("Time Help to Fade " + (TimeToDisableHelp % 60).ToString("F0") + "s");
}
else if (TimeToDisableHelp < 3600){
GUILayout.Label("Time Help to Fade " + ((TimeToDisableHelp/60) % 60).ToString("F0") + "min " + (TimeToDisableHelp % 60).ToString("F0") + "s");
}
else if (TimeToDisableHelp < 86400){
GUILayout.Label("Time Help to Fade " + ((TimeToDisableHelp/ 3600) % 24).ToString("F0") + "Hour " + ((TimeToDisableHelp/60) % 60).ToString("F0") + "min " + (TimeToDisableHelp % 60).ToString("F0") + "s");
}
// else if (false){
// TimeToDisableHelp = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}",displayDays,displayHours, displayMinutes, displaySeconds);
// }// nothing works here
TimeToDisableHelp = GUILayout.HorizontalScrollbar(TimeToDisableHelp,0.01f, 0,TimeDisableHelpSliderMax ,GUILayout.Width(50));
GUILayout.Label("Time help whole number: " + TimeToDisableHelp.ToString("F0") );
GUILayout.Label("Bunny's way " + TimeToDisableHelp.ToString("yyyy-MM-dd_HH:mm:ss") );
for my code I get depending on size of TimeToDisableHelp:
Time Help to Fade 2min 30s
Time help whole number: 90
Bunny's way yyyy-MM-dd_HH:mm:ss // that's all nothing changes
but if I have: 89
Time Help to Fade 1min 29s
Time help whole number: 89
but if I have: 5409
Time Help to Fade 2hours 30min 9s
Time help whole number: 5409
but if I have: 5339
Time Help to Fade 1hours 30min 59s
Time help whole number: 5339
I don't have smallest idea what % does and how to solve this.
Answer by TonyLi · Apr 01, 2013 at 05:43 PM
How about replacing all that with TimeSpan.FromSeconds().ToString()? The line below converts TimeToDisableHelp to a general-format string and assigns it to s.
string s = TimeSpan.FromSeconds(TimeToDisableHelp).ToString("");
TimeSpan reference: http://msdn.microsoft.com/en-us/library/system.timespan.aspx
ToString() method: http://msdn.microsoft.com/en-us/library/dd992632.aspx
String formats: http://msdn.microsoft.com/en-us/library/ee372286.aspx
Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(382,46): error CS0103: The name `TimeSpan' does not exist in the current context
than I remembered I saw it in 1. one
than I included using system and now:
Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(383,86): error CS1501: No overload for method `ToString' takes `1' arguments
for
string str = TimeSpan.FromSeconds(TimeToDisableHelp).ToString("g");
So does it work if you include the System namespace (using System) or change that line to:
string str = System.TimeSpan.FromSeconds(TimeToDisableHelp).ToString("g");
no I still get:
string str = System.TimeSpan.FromSeconds(TimeToDisableHelp).ToString("g");
Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(395,93): error CS1501: No overload for method `ToString' takes `1' arguments
Sorry, I wasn't near Unity to test. The version of ToString() that takes arguments is .NET 4.0, and Unity is essentially .NET 3.5, so you can only call ToString() without arguments. It returns a string of the form:
[-][d.]hh:mm:ss[.fffffff]
where the parts in square brackets will only appear if necessary. So you'll most likely get something of the form hh:mm:ss.
Here's the reference page for the .NET 3.5 version: http://msdn.microsoft.com/en-us/library/vstudio/1ecy8h51(v=vs.90).aspx
ok got it now but I don't know where to put this
[-][d.]hh:mm:ss[.fffffff]
or how to get rid of 0.000000 sec
or how to make ins$$anonymous$$d
":" to "$$anonymous$$" or "hours"
can you edit your answer and remove that "g" ?
Your answer
Follow this Question
Related Questions
How to convert timer to minutes, seconds and cents 3 Answers
Multiple Cars not working 1 Answer
String format to show float as time 3 Answers
Distribute terrain in zones 3 Answers
Fade in AudioLister over time #c 1 Answer