Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to get the Real World Current Time?

Discussion in 'Scripting' started by Senkusha, Apr 4, 2011.

  1. Senkusha

    Senkusha

    Joined:
    Mar 28, 2011
    Posts:
    98
    Is there a way within Unity to get the real world current time? Like from the computer system clock?

    Thanks!
     
  2. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    System.DateTime.Now should do it.

    Jeff
     
  3. Senkusha

    Senkusha

    Joined:
    Mar 28, 2011
    Posts:
    98
    Awesome! Is there anyway to convert that DateTime string into Seconds from midnight?

    Thanks!
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
  5. Senkusha

    Senkusha

    Joined:
    Mar 28, 2011
    Posts:
    98
    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!
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    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
     
  7. RodrigoSeVeN

    RodrigoSeVeN

    Joined:
    Jul 10, 2010
    Posts:
    15
    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.
     
  8. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    PotatoMasha likes this.
  9. RodrigoSeVeN

    RodrigoSeVeN

    Joined:
    Jul 10, 2010
    Posts:
    15
    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)
    Code (csharp):
    1.  
    2. function checkGlobalTime(){
    3.     var url = "http://www.nanonull.com/TimeService/TimeService.asmx/getUTCTime";
    4.     var request = WWW(url);
    5.     yield request;
    6.  
    7.     if(request.error!=null) return;
    8.         else{
    9.         parser=new XMLParser(); //~ This belongs to that post/link up there.
    10.         var globalTimeXML=parser.Parse(request.data);
    11.         //~ globalTimeXML["string"][0]["_text"]) will be equals to "2:34 PM" for example.
    12.             var newGlobalTime=Regex.Split(globalTimeXML["string"][0]["_text"]," ");
    13.         }
    14. }
    15.  
    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.