DateTime Comparisons: Error

Hi there,
I would like to know time passed between Start() and Start(). What I mean is, if I open the program today at 3:35pm, close it. The reopen it at 4:45pm, tomorrow, I want to know the difference in time passed.

I am using this code, but it gives me an error at line 7
Error:

Code:

       //TIMEpassedsincelastplayed...
//Storethecurrenttimewhenit starts
currentDate = System.DateTime.Now;

//Grabtheoldtimefromtheplayerprefsasa long
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));

//ConverttheoldtimefrombinarytoaDataTime variable
DateTime oldDate = DateTime.FromBinary(temp);
print("oldDate:" + oldDate);

//UsetheSubtractmethodandstoretheresultasatimespan variable
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference:" + difference);

Where do you set the datetime into the PlayerPrefs? Are you using DateTime.ToBinary?

I just did a little test, and this seems to work:

using System;

// get current date
DateTime date = DateTime.Now;

// convert to binary
long date_bin = date.ToBinary();

// save binary as string
PlayerPrefs.SetString("date", date_bin.ToString());

// get the binary back from the save data
long old_date_bin = long.Parse(PlayerPrefs.GetString("date"));

// convert from binary to datetime.
DateTime oldDate = DateTime.FromBinary(old_date_bin);

// get the time difference
TimeSpan ts = date - oldDate;

// get the difference in days
int differenceInDate = ts.Days;

Hey man, thanks.

However, it does not seem to be doing anything.

I adjusted the int differenceInDate to a float, and from Days to Seconds. Other than that, everything is the same. However, when starting the program, turning off the program and starting it again, there is no difference in time, “differenceInDate = 0”.

???

Here is my code…

//IN START()

 //getcurrent date
 DateTime date = DateTime.Now;
 
 //convertto binary
 long date_bin = date.ToBinary();
 
 //savebinaryas string
 PlayerPrefs.SetString("date", date_bin.ToString());
 
 //getthebinarybackfromthesave data
 long old_date_bin = long.Parse(PlayerPrefs.GetString("date"));
 
 //convertfrombinarytodatetime.
 DateTime oldDate = DateTime.FromBinary(old_date_bin);
 
 //getthetime difference
 TimeSpan ts = date - oldDate;
 
 //getthedifferencein days
 float differenceInDate = ts.Seconds;
 print("SECONDS=" + differenceInDate);
   public void OnApplicationQuit(){
 PlayerPrefs.SetString("date", DateTime.Now.ToBinary().ToString());
 PlayerPrefs.Save();

 }
 //savebinaryas string
PlayerPrefs.SetString("date", date_bin.ToString());
//getthebinarybackfromthesave data
long old_date_bin = long.Parse(PlayerPrefs.GetString("date"));

You overwrite your old date with the current date in your PlayerPrefs right before you attempt to get the old date.

1 Like

Yes that was just for an example of storage & retrieval of the DateTime.

Also Seconds, Milliseconds, etc are all int not float.

https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx?f=255&MSPPError=-2147217396

If you want the difference in fractional seconds, use TotalSeconds.

using System;

// get the binary back from the save data
long old_date_bin = long.Parse(PlayerPrefs.GetString("date"));

// convert from binary to datetime.
DateTime oldDate = DateTime.FromBinary(old_date_bin);

// get current date
DateTime currentDateTime = DateTime.Now;

// get the time difference
TimeSpan ts = currentDateTime - oldDate;

// get the total difference in seconds
double difference = ts.TotalSeconds;

Debug.Log(difference);

// save current time for next run

// convert current time to binary
long date_bin = currentDateTime.ToBinary();
// save binary as string
PlayerPrefs.SetString("date", date_bin.ToString());
1 Like

RIGHT!!!

Thank you!!!