- Home /
Save current time on quit, then on relaunch compare to new current time and get difference?
Hi all!
Bit of a hefty question today, I'm looking to get the difference between two different times, one time being when the application was last quit, and when it was opened again. I've tried a few different methods but with no real success.
My current method involves recording the binary time (a long int) into player prefs (which requires converting into a string) and calling that back out on opening the application as a string, and converting it back to a long. This works up until a point, which is when converting it back yields the error "Cannot implicitly convert type string to long"
Here's the current code all nice and commented and stripped of irrelevant lines
using UnityEngine;
using System.Collections;
public class DataMaster : MonoBehaviour {
public long sysTime;
public long lastTime;
public long difTime;
void Start () {
//This line yields the "Cannot implicitly convert type string to long".
//It should recall the the binary time out of the player prefs and hold it as the long variable 'lastTime'
long lastTime = long.Parse(PlayerPrefs.GetString ("sysString"));
long difTime = (lastTime-sysTime);
}
void Update ()
{
//This fetches the time, turns it into its binary form and sends it to the sysTime long
sysTime = System.DateTime.Now.ToBinary();
}
void OnApplicationQuit ()
{
PlayerPrefs.SetString("sysString", sysTime.ToString());
}
}
If anyone has a method to solve this, or a whole new method it would be greatly appreciated. I'm open to totally different ways of doing this, as long difTime ends up as a number I can use in further operations I'm a happy man ^_^
Thanks everyone!!
Answer by Default117 · Sep 28, 2011 at 03:13 PM
You're going to want to read up on the DateTime class as Jason mentioned. It has a ToBinary method which you have used to convert the time to a long. Do the reverse of this when you want to convert your binary data back to a long. Which is 'FromBinary'.
C# has a nice class called "Convert", which has a few methods to convert one data type to another. In this case you want to convert the string from player prefs to a long (Int64). Now you can use the FromBinary to get the DateTime.
Once you have your old DateTime, you can use it's 'Subtract' method to subtract the new from the old. This gets stored as a TimeSpan variable. So check that out, and use it how you wish. See the below example.
Be careful though! As you can see, you're grabbing the old date from player prefs and then trying to calculate the difference. But what if this is the first playthrough? You need to take this factor into account. Perhaps set the old date to current date if there is none? Resulting in a difference of 00:00:00.
Enjoy!
using UnityEngine;
using System.Collections;
using System;
public class DataMaster : MonoBehaviour
{
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);
}
}
You can always use PlayerPrefs.Has$$anonymous$$ey(""))
to check if it is the first playthrough ^^
10 years later and you are still preventing mental breakdowns. Thanks!
Answer by Jason B · Sep 28, 2011 at 02:09 PM
Add using System;
to the top.
From there, you can start easily using DateTime objects to store, restore, and compare dates.
For instance:
DateTime currentDate;
currentDate = DateTime.now;
Would get today's date and store it in variable currentDate.
And these DateTime objects have all sorts of yummy things to play with, including a "DateTime.CompareTo" function. So you could do something like currentDate.CompareTo(lastDate);
and get something useful out of that.
I'd recommend doing more reading about DateTime on other C#-related sites, but this is what I've been using and it works well.
Thanks for the input! The above answer ended up solving it but you make a good point regardless, there's lots of trawling of $$anonymous$$SDN and such to be done for C# :D
Answer by Alucard-Belmont · Aug 18, 2018 at 06:08 AM
Please help.
I need to convert this value of the difference obtained in int to the number of total seconds. I already tried to do it in many ways and I can not.
I hope you can help me.
Regards!!
When you have a question, please ask a question and do not post an answer that doesn't answer the question.
Your answer
Follow this Question
Related Questions
How to make notification appear at certain time without having to press button again? 2 Answers
Multiple Cars not working 1 Answer
Check How Many Minutes Have Passed 1 Answer
Distribute terrain in zones 3 Answers
Countdown timer into text c# 1 Answer