- Home /
Getting the current real life time?
Hi! I've looked a bit in the scripting reference manual, especially around the Time class. I'm looking to find a way to get the current real life time, for example the time of the clock on the computer, like 14:34, or even just time of the day in seconds. All I could found was "time since game start", which is not what i want. Any ideas?
I used google, but I was dumb enough to search for "unity3d real life time", which didn't work quite well x).
Answer by ShinAli · Sep 18, 2011 at 09:20 AM
Don't forget that you also have the Mono system libraries at hand (mostly an implementation of .NET 3.5). There is a structure under the System namespace called DateTime, with it a member called Now which returns a DateTime structure of the current time. So, in code:
// in C#
using System;
DateTime now = DateTime.Now;
// in JavaScript (forgive me if this is wrong, I don't use JS often)
var now : System.DateTime;
now = System.DateTime.Now;
For more information to the DateTime structure, refer to the .NET library documentation over here or the Mono System library documentation over here.
Doesn't time value co$$anonymous$$g from net function change when device time is set manually?
Answer by Krayt · Sep 18, 2011 at 08:16 AM
Took me a bit to remember how to do this...this next bit of code will basically create a variable of Type DateTime (available under the System namespace - not System.Collections) and then output the time to the console. You can do alot with this class and any objects instantiated from it, so you play around with the formatting til you get something you can use.
System.DateTime myTime = System.DateTime.Now;
Debug.Log(myTime.TimeOfDay);