x


Making a clock show the system time

I'm making a clock and want it to show the system time of whatever computer the game is running on. Could anyone give me a pointer on how to do this?

more ▼

asked Nov 14 '10 at 01:54 PM

pauldstewart gravatar image

pauldstewart
187 28 36 48

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Are you using C#? As long as you are using System you should have access to the DateTime libraries.

more ▼

answered Nov 14 '10 at 02:23 PM

Adam Rademacher gravatar image

Adam Rademacher
1.1k 1 3 19

I didn't see that in the included libraries. Unity does not include every namespace from the .Net framework. Have you checked that it is there?

Nov 14 '10 at 02:27 PM Peter G

It's there in all versions of the .NET frameworks in unity. Also - you can use them in javascript too Adam, c# doesn't matter

Nov 14 '10 at 02:31 PM Mike 3

I'm using javascript. Where would I find the DateTime libraries?

Nov 14 '10 at 06:17 PM pauldstewart

Check the link for 'datetime libraries' in my answer.

Nov 15 '10 at 01:46 AM Adam Rademacher
(comments are locked)
10|3000 characters needed characters left

Here's a simple script I wrote in response to your question. This should get you started. This example will show the current system time in 12 hour (am/pm) format. This example is in JavaScript (UnityScript). Just attach this script to either the main camera, or some empty game object in scene. This will show the time both on the GUI, and will print to the console.

#pragma strict

    private var time : System.DateTime;
    private var h : float;
    private var m : float;
    private var s : float;
    private var format : String = "hh:mm:ss";
    private var ampm : String;

    function Update () 
    {
       time = System.DateTime.Now;
       h = time.Hour;
       m = time.Minute;
       s = time.Second;
       if(h > 0 && h < 12)
         ampm = " AM";
       else if (h > 11 && h < 24)
         ampm = " PM";
       else if(h == 0)
        ampm = " AM";
       print (h);
    }

    function OnGUI()
    {
       GUI.Box(Rect(Screen.width /2, Screen.height /2, Screen.width /10, Screen.height /10), time.ToString(format)+ampm);
    }
more ▼

answered Dec 17 '12 at 05:58 AM

clunk47 gravatar image

clunk47
2.7k 2 8

Remember not to declare variables outside functions unless necessary. Also you don't need to compute AM/PM yourself, use string formatting instead. The script could be written like this:

function OnGUI()
{
   GUI.Box(Rect(Screen.width/2, Screen.height/2, 200, 30),
           System.DateTime.Now.ToString("h:mm:ss tt"));
}
Dec 17 '12 at 07:57 AM Eric5h5

Thx for the additional help @Eric5h5 :)

Dec 17 '12 at 04:08 PM clunk47
(comments are locked)
10|3000 characters needed characters left

This was a piece of javescript that I was using to grab the date once a long time ago I modified it to do the time.

This Link will give you all the different ways you can use System.DateTime in a string

http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

//sets something to record the date and time
var date : System.DateTime;

//sets something allowing you to populate it with the date and time
var text : String;


function Update()
{
    //tells the var to look for the precise moment you are at
    var date = System.DateTime.Now;
    //converts the above var to a readable string
    text = date.ToString("HH:mm:ss");
    //prints it to inspector
    print (text);

}
more ▼

answered May 10 '12 at 01:07 PM

alauger07 gravatar image

alauger07
0 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x570
x20

asked: Nov 14 '10 at 01:54 PM

Seen: 2084 times

Last Updated: Dec 17 '12 at 04:08 PM