Is there a way within Unity to get the real world current time? Like from the computer system clock?
Thanks!
Is there a way within Unity to get the real world current time? Like from the computer system clock?
Thanks!
System.DateTime.Now should do it.
Jeff
Awesome! Is there anyway to convert that DateTime string into Seconds from midnight?
Thanks!
DateTime.Now is a DateTime object not a string, it has all kinds of properties and methods…
Hi again,
I tried to access the System.DateTime.Minute method but I get an error (Is not a member…). Is this method available in JavaScript, or do I need to convert my code to C# to get the number of minutes in the current day from midnight?
Thanks!
All Mono/.NET classes are available to all languages, they’re not related to C#. Have a look at the docs for how you use Minute. (You wouldn’t say System.DateTime.Minute; rather Minute is a property of a DateTime object. i.e., you need to give it something to get the minute from.)
–Eric
Senkusha, you missed one word when using the code: Now.
It should be System.DateTime.Now.Minute. There’s also System.DateTime.UtcNow.
Last but not least, this is, as it states, the time in the system, which means it’s bound to be variable from machine to machine. If the player changes manually the system time, you could get in trouble. Just re-tested this, it happens. Which is why, since i’m working on a webplayer application, i’m searching for a global, internet based clock to access. I’m looking for any tips on that.
@SeVeN
How about that?
http://www.nanonull.com/TimeService/TimeService.asmx
http://www.nanonull.com/TimeService/TimeService.asmx/getUTCTime
You will of coures have some lag, but as far as you can work with +/- 1 second difference everything should be ok.
I think we are still on the subject so I’ll just go ahead.
@Marrrk
I used the WWW class to get the XML data and then I used the XMLParser from this post: http://forum.unity3d.com/threads/25352-Loading-an-XML-file?p=241374&viewfull=1#post241374 and the link on it. With this I managed to get a string like: 2:34 PM
Have you possibly used another approach on this that is simpler, since from that string up there, i will need to split the string twice, so i get the period of the day and the hour/minutes?
I’ll put the whole code here just in case: (This was done in Unity 2.6 so the “request.data” may have been replaced by “request.text” on Unity 3)
function checkGlobalTime(){
var url = "http://www.nanonull.com/TimeService/TimeService.asmx/getUTCTime";
var request = WWW(url);
yield request;
if(request.error!=null) return;
else{
parser=new XMLParser(); //~ This belongs to that post/link up there.
var globalTimeXML=parser.Parse(request.data);
//~ globalTimeXML["string"][0]["_text"]) will be equals to "2:34 PM" for example.
var newGlobalTime=Regex.Split(globalTimeXML["string"][0]["_text"]," ");
}
}
newGlobalTime[0] will be equal to 2:34 and newGlobalTime[1] will be PM. After that i will split newGlobalTime[0] with “:” again to get hours minutes. Now with three values, i would parse the numbers and would compare the “PM” to figure out the period of the day, etc.
It feels like an expensive process to get such a simple result, but it’ll be fine in my case if no better solutions appear.