- Home /
Time formatting
Hey everyone,
Currently in my game I'm working on a timer. The biggest problem I'm having is with the way it's displayed and I have no idea how to format it properly. I've searched through Unity and found stuff from 2006 which is quite outdated. Currently the time is displaying "1.40202392" and I want it to show "minutes:seconds" example "1:30." Any help is welcome and much appreciated! Thank you
Slyy
Answer by paulaceccon · Nov 15, 2012 at 04:45 PM
In C# there a format function that allows you to do something like this: string text = string.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
I don't know much about JavaScritp, but maybe this can help you:
http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript
http://blog.stevenlevithan.com/archives/date-time-format
http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss
In JS, the code could be like this (assu$$anonymous$$g that this script is assigned to a GUIText object):
var secs: int = Time.time % 60; var $$anonymous$$s: int = Time.time / 60; guiText.text = String.Format("{0:00}:{1:00}", $$anonymous$$s, secs);
String.Format is not a C# feature, it's a .NET feature and works in all languages in Unity.
Answer by markpdolby · Nov 15, 2012 at 04:45 PM
string formattedTime = (int)(timer / 60) + ":" + (int)(timer % 60)
Off the top of my head something like that should work or at least point you in the right direction.
Answer by FakeBerenger · Nov 15, 2012 at 04:47 PM
A script I did a while ago. It should do the trick for you : http://wiki.unity3d.com/index.php/TimeOfDay
Your answer
Follow this Question
Related Questions
Timer Pause 1 Answer
how to find time of a button pressed 2 Answers
How to make the tab key to show/hide GUIlayer 1 Answer
Change timescale to zero but still have GUI textures moving 1 Answer
Time.timeScale = 0 breaks my GUI. 0 Answers