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.

1 Answer

1

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: TimeSpan Struct (System) | Microsoft Learn

ToString() method: TimeSpan.ToString Method (System) | Microsoft Learn

String formats: Standard TimeSpan format strings | Microsoft Learn

Assets/Scripts/MainMenu.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/MainMenu.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/MainMenu.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 instead ":" to "min" or "hours" can you edit your answer and remove that "g" ?

–