Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to count the offline time

Discussion in 'Scripting' started by LetmeDwight, Dec 14, 2020.

  1. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    I want to make a app, that can remebers how many days the last run of this apk was.
    I think I have to workin with somthing like DateTime.
    But how can I compare the datetime values from 4 days ago with the current work in order to then execute an if accordingly if the app has not been used for more than 4 days, which is calculated from the distance between the last Datetime.Now and the new one. that results when the app is started for the first time. The old value is retained and saved by this asset:

    But how can I compare the datetime values from 4 days ago with the current work in order to then execute an if accordingly if the app has not been used for more than 4 days, which is calculated from the distance between the last Datetime.Now and the new one. that results when the app is started for the first time. The old value is retained and saved by this asset:
    https://assetstore.unity.com/packages/tools/input-management/save-game-free-gold-update-81519
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You can subtract one DateTime from another to get a TimeSpan representing the elapsed time between them.
     
    Bunny83 likes this.
  3. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't quite understand what you're asking. But whenever I need to determine the amount of time the game has been closed, I prefer using DateTime.Now.Ticks. It is trivial to convert to seconds, hours, days, etc, and I find doing my own simple math with just a "long" is a lot faster and easier to wrap my head around than dealing with the rest of the DateTime class.
     
    Last edited: Dec 15, 2020
  5. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    you get accurate results doing that even when changing machines ?
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    According to this, the number of DateTime "ticks" per second is a constant, not machine-dependent.
     
    _met44 likes this.