Search Unity

Getting Current Time As A Float C#

Discussion in 'Scripting' started by VaunMakuza, Aug 8, 2014.

  1. VaunMakuza

    VaunMakuza

    Joined:
    Sep 20, 2012
    Posts:
    40
    Is there a way of getting the current time as a float?

    Don't necessarily need the date as well, just the current time (I.E 8:00). I tried messing around with DateTime but either I'm not seeing a way to do it, or its not possible with DateTime!

    Any help would be greatly appreciated!

    Thanks in advance.
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    System.DateTime.Now should return the current time.

    Edit: Realized that you want this as float. Depends on how you want the conversion. The way we represent time is not directly equivalent to one number, unless you want the time in Number of Seconds.
     
    Last edited: Aug 8, 2014
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Probably something like;

    Code (CSharp):
    1. ((float)DateTime.Now.Hour + ((float)DateTime.Now.Minute * 0.01f))
     
  4. VaunMakuza

    VaunMakuza

    Joined:
    Sep 20, 2012
    Posts:
    40
    That is actually pretty damn close to being perfect! It captured "11:2" and it was 11:21, so thats pretty awesome. Thank you very much!
     
    mcbauer likes this.
  5. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Why do you need it as a floating point value?
     
  6. VaunMakuza

    VaunMakuza

    Joined:
    Sep 20, 2012
    Posts:
    40
    Game Analytics requires a float in their hooks to be sent to the server.
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240