- Home /
How many hours less than 24 has it been since last played
I want to check how long it has been since the player last played in hours minutes and seconds and for that time to be added onto another timespan and taken away from 24 hours. I tried this:
DateTime currentDate;
DateTime oldDate;
void Start()
{
//Store the current time when it starts
currentDate = System.DateTime.Now;
//Grab the old time from the player prefs as a long
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
//Convert the old time from binary to a DataTime variable
DateTime oldDate = DateTime.FromBinary(temp);
print("oldDate: " + oldDate);
//Use the Subtract method and store the result as a timespan variable
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference: " + difference);
}
void OnApplicationQuit()
{
//Savee the current system time as a string in the player prefs class
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs: " + System.DateTime.Now);
}
but it only told me how long the player had played since the last time. I need it to add that time to a different timespan and save that timespan with Playerprefs. I searched all over the internet to find a solution but couldn't find it anywhere. This sounds so simple yet I cant find the solution.
Comment
Your answer
Follow this Question
Related Questions
See if a time span is bigger than X 4 Answers
Help with using keys to activate scripts 2 Answers
Accumulating deltaTime oddity 1 Answer
Unity Simple Clock Tutorial with custom time? 1 Answer
Subtract milliseconds from 12 hours? 1 Answer