SecondsToText

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<csharp>" to "<syntaxhighlight lang="csharp">")
m (Text replace - "</csharp>" to "</syntaxhighlight>")
Line 97: Line 97:
 
     return outString;
 
     return outString;
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
== Usage ==
 
== Usage ==
Line 109: Line 109:
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
string timeAsText = textTime(Time.time);
 
string timeAsText = textTime(Time.time);
</csharp>
+
</syntaxhighlight>
  
 
Time.time being replaced by your own variable in seconds.
 
Time.time being replaced by your own variable in seconds.

Revision as of 20:45, 10 January 2012

Contents

Summary

This is a simple script which can be used to convert a time value in seconds into a readable text string. e.g. 15 => 15 seconds, or 3601 => 1 hour etc...

Script (Javascript)

<javascript> function textTime (seconds : int) {

   if (seconds < 0) {
       return '0';
   }
   if (seconds < 60) {
       n = seconds;
       return n + ' second' + s(n);
   }
   if (seconds < 60 * 60) {
       n = Mathf.Floor(seconds/60);
       return n + ' minute' + s(n);
   }
   if (seconds < 60 * 60 * 24) {
       n = Mathf.Floor(seconds/60/60);
       return n + ' hour' + s(n);
   }
   if (seconds < 60 * 60 * 24 * 7) {
       n = Mathf.Floor(seconds/60/60/24);
       return n + ' day' + s(n);
   }
   if (seconds < 60 * 60 * 24 * 31) {
       n = Mathf.Floor(seconds/60/60/24/7);
       return n + ' week' + s(n);
   }
   if (seconds < 60 * 60 * 24 * 365) {
       n = Mathf.Floor(seconds/60/60/24/31);
       return n + ' month' + s(n);
   }
   n = Mathf.Floor(seconds/60/60/24/365);
   return n + ' year' + s(n);

}

function s (n : float) {

   return n == 1 ?  : 's';

} </javascript>

Script (CSharp)

string s (int n) {
    	if (n == 1) {
    		return (" ");
    	} else {
    		return ("s");
    	}
}
 
string textTime (float seconds) {
		int n = 0;
		string outString = "error";
    	if (seconds < 0) {
        	return "0 seconds";
    	}
    	if (seconds < 60) {
    	    n = (int)Mathf.Floor(seconds);
    	    outString = n + " second" + s(n);
    	    return outString;
    	}
    	if (seconds < 60 * 60) {
    	    n = (int)Mathf.Floor(seconds/60);
    	    outString = n + " minute" + s(n);
    	    return outString;
    	}
    	if (seconds < 60 * 60 * 24) {
    	    n = (int)Mathf.Floor(seconds/60/60);
    	    outString = n + " hour" + s(n);
    	    return outString;
    	}
    	if (seconds < 60 * 60 * 24 * 7) {
     	    n = (int)Mathf.Floor(seconds/60/60/24);
    	    outString = n + " day" + s(n);
    	    return outString;
    	}
    	if (seconds < 60 * 60 * 24 * 31) {
    	    n = (int)Mathf.Floor(seconds/60/60/24/7);
    	    outString = n + " week" + s(n);
    	    return outString;
    	}
    	if (seconds < 60 * 60 * 24 * 365) {
    	    n = (int)Mathf.Floor(seconds/60/60/24/31);
    	    outString = n + " month" + s(n);
    	    return outString;
    	}
    	n = (int)Mathf.Floor(seconds/60/60/24/365);
    	outString = n + " year" + s(n);
    	return outString;
}

Usage

Place anywhere in your monobehaviour and call using something similar to:

JS: <javascript> var timeAsText : String = textTime(Time.time); </javascript> C#

string timeAsText = textTime(Time.time);

Time.time being replaced by your own variable in seconds.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox