SecondsToText

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
Line 45: Line 45:
 
     return n == 1 ? '' : 's';
 
     return n == 1 ? '' : 's';
 
}
 
}
</javascript>
+
</syntaxhighlight>
  
 
== Script (CSharp) ==
 
== Script (CSharp) ==
Line 105: Line 105:
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
var timeAsText : String = textTime(Time.time);
 
var timeAsText : String = textTime(Time.time);
</javascript>
+
</syntaxhighlight>
 
C#
 
C#
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">

Latest revision as of 20:52, 10 January 2012

Contents

[edit] 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...

[edit] Script (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';
}

[edit] 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;
}

[edit] Usage

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

JS:

var timeAsText : String = textTime(Time.time);

C#

string timeAsText = textTime(Time.time);

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

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox