S
S
Sergey Ivanov2019-03-22 14:25:36
C++ / C#
Sergey Ivanov, 2019-03-22 14:25:36

Unity time away calculation?

private void OnApplicationQuit()
    {
        Save();
    }
    public void Save()
  {
        PlayerPrefs.SetString("Last", DateTime.Now.ToString());
}

and then calculation
TimeSpan ts = DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("Last"));
            float time = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
            TheTime -= time;

why does it calculate correctly only if more than a minute has passed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2019-03-22
@keksmr

float time looked exactly? or immediately TheTime??
(although the local format for saving time probably does not take into account seconds. It saves only minutes in a string) it’s almost certainly a matter of the format))
when they checked with the server, the time was stored in milliseconds.
and also in UTC, so that there are no problems with the local time format, etc. just the same number for all)

//save
long currenttime = System.DateTime.UtcNow.Ticks;
PlayerPrefs.SetString("key", currenttime.ToString());

//load
long savedTime = long.Parse(PlayerPrefs.GetString("key", currenttime.ToString()));
System.TimeSpan timeSpan = System.DateTime.UtcNow - new System.DateTime(savedTime);

S
shai_hulud, 2019-03-22
@shai_hulud

PlayerPrefs.Save() are you doing?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question