Search Unity

How to get system time in Unity?

Discussion in 'Scripting' started by daqulazhang, Sep 2, 2009.

  1. daqulazhang

    daqulazhang

    Joined:
    Jun 13, 2009
    Posts:
    33
    hi,
    I want to know current system time in client side(the Unity) to sync with server side. I checked the Time class but seems there's no such variable in it.
    Does anyone know about this?
    thanks in advance.

    -daqula
     
  2. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Use the .Net Library to get the system time :)
     
  3. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    You'll need to use the Mono System.DateTime class. To get the current time, you simply query
    Code (csharp):
    1. System.DateTime.Now
    If you'll be using it often, you'll need to import it
    Code (csharp):
    1. import System
    and then you can just refer to it as "DateTime" and "DateTime.Now" when you use the class.

    The docs for DateTime can be found here. Do make a note of the note on the page, it does not use Unix time. If you need to compare against that, you'll probably want to convert it to UTC time, and use System.DateTime.UTCNow and compare those.
     
  4. daqulazhang

    daqulazhang

    Joined:
    Jun 13, 2009
    Posts:
    33
    thanks both of you:)
     
  5. skyscream

    skyscream

    Joined:
    Jan 28, 2010
    Posts:
    43
    and how can I get only hour from this?
     
  6. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    DateTime.Hour
     
  7. dsjunnesson

    dsjunnesson

    Joined:
    Dec 3, 2012
    Posts:
    15
    Here is a short example on how you would get the UTC value.

    System.DateTime.UtcNow.ToString()

    And here is if you want to get a String back formatted in some specific way

    System.DateTime.UtcNow.ToString("HH:mm dd MMMM, yyyy") // returns "12:48 15 April, 2013"
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  9. rahulreddyv

    rahulreddyv

    Joined:
    Mar 11, 2014
    Posts:
    2
    Hi, I require accessing milliseconds for my program. How can I do it?
     
  10. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
  11. the code cracker

    the code cracker

    Joined:
    Jun 30, 2014
    Posts:
    2
    does this work on all platforms (ios android...)?
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  13. the code cracker

    the code cracker

    Joined:
    Jun 30, 2014
    Posts:
    2
    thx
     
  14. HamFar

    HamFar

    Joined:
    Nov 16, 2014
    Posts:
    89
    Is there a way of registering time when you exit a Unity application (i.e. you press play and stop the app) or perhaps when you exit a standalone build?

    Using the following simple line of code, I can write to the console when my start button was pressed, so I wonder if I could do the same when the app is quit, so that the difference would be the length of the session?

    Code (CSharp):
    1. Debug.Log("Start Button pressed @ " + DateTime.Now);
     
    BitGamey likes this.
  15. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  16. HamFar

    HamFar

    Joined:
    Nov 16, 2014
    Posts:
    89
    Thank you! Just what I needed...
     
  17. Nani027

    Nani027

    Joined:
    Jan 5, 2017
    Posts:
    2
    Is there any way wherein time still working even the game is off?
     
  18. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Eh what is you specific use case? Getting the current time in-game while the game is not running is obviously not possible.
     
  19. Nani027

    Nani027

    Joined:
    Jan 5, 2017
    Posts:
    2
    My game is like candy crush. The energy regenerates even the game is not active as well as the time. Can you help me please? Just for educational purpose. Thank you.
     
  20. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You can't do anything when the game's off. That's kind of the point of the game being off!

    The easiest thing is to store the time when you turn the game of (PlayerPrefs is adequate for this), and then check it when you turn it back on again.

    Of course, it'd be really easy to cheat, as the playerprefs are stored on the users device and is changeable. If you want to avoid that, you'll have to have an online server where you store the current state of the user's game. If it's for educational purposes, don't worry about that.
     
    Michael4545, Zyke and HamFar like this.
  21. MerhuBerahu

    MerhuBerahu

    Joined:
    Jun 29, 2015
    Posts:
    2
    Could you use the .NET to get the current time, set a "world starting time" to say 01 jan 1990(or when you first start the game), and work how how much time has passed since then?

    adding on "candy" or w/e incrementally each day?

    (ofc this would be abuse-able with changing the system clock, much like fallout shelter, etc)
     
  22. Deleted User

    Deleted User

    Guest


    How I can change the date language into another language? It's default is in English. How can I change it to French? For that example "15 Avril 2013" instead of 15 April 2013?
     
  23. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You'll want to specify the fr-FR culture in the ToString method. The documentation with examples are here.
     
  24. DimaIshenko1993

    DimaIshenko1993

    Joined:
    Jan 4, 2017
    Posts:
    1
    How to know what current day is?
     
  25. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome. As you can see right at the bottom (which is kinda stupid layout imho), DateTime has a property for Day. Or Month. Or Year. Or basically anything else you can get from it, so you can easily access any particular part of the time saved in it: https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8

    Generally speaking, it's always worth to just have a quick look at the interface of such structs to see what they do. So a quick google search can, in most cases, give you the result a lot faster compared to asking on the forum. Of course, if you didnt find anything, asking is always fine.
     
    Blaztek likes this.
  26. ImposterOfTheVent

    ImposterOfTheVent

    Joined:
    Apr 11, 2021
    Posts:
    1
    In the line System.DateTime.UtcNow.ToString("HH:mm dd MMMM, yyyy") get rid of the :mm dd MMMM, yyyy
     
  27. kenanimohamed

    kenanimohamed

    Joined:
    Jan 19, 2021
    Posts:
    7
    hi please i need to make a countdown from NOW (this time) to midnight. i really cant find a way
     
  28. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Please do not reply to 12-year-old threads with unrelated questions. This is against forum rules.

    Instead, start your own post... it's free!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  29. snowmobeetle

    snowmobeetle

    Joined:
    May 23, 2017
    Posts:
    7
    Relevant Answer using Unity 2019.2.13:

    Added this function to one of my c# MonoBehavior Unity scripts and reference it elsewhere when I want the build time to be displayed on an info screen. OnValidate runs when there's any change in unity or when build is run. Doesn't need anything else, OnValidate only runs in the editor environment while NOT in play mode. Works perfectly for me and is super simple. It's even visible in the inspector so you can see the format. Use string formatting if you want to clean it up.


    Code (CSharp):
    1.     public string autoGeneratedBuildTime;
    2.  
    3.     private void OnValidate()
    4.     {
    5.         autoGeneratedBuildTime = DateTime.Now.ToString();
    6.     }
     
  30. Korvacs

    Korvacs

    Joined:
    Feb 28, 2021
    Posts:
    2
    This was kind of answered by djunnesson but you can output time only

    i.e.:

    DateTime.Now.ToString("hh:mm tt");

    Will output something like 01:57 PM

    DateTime.Now.ToString("HH:mm");

    Will output something like 13:57
     
    Last edited: Sep 9, 2022