- Home /
Formatting Exception when using DateTime for PlayerPrefs
Hi i need help, i am getting a FormatException when i try the below codes, it will save datetime and save to playerprefs then on resume will get the difference from the old time and current time, but i am having an Format exception, anyone who knows how to convert these things without errors? thank you
import UnityEngine; import System.Collections; import System;
var currentDate : DateTime; var oldDate : DateTime; var timex : double;
function Start() {
//Store the current time when it starts
currentDate = System.DateTime.Now;
//Grab the old time from the playerprefs as a long
var temp : long = Convert.ToInt64(PlayerPrefs.GetString("Time_Data"));
//Convert the old time from binary to a DataTime variable
var oldDate : DateTime = DateTime.FromBinary(temp);
print("oldDate: " + oldDate);
//Use the Subtract method and store the result as a timespan variable
var difference : TimeSpan = currentDate.Subtract(oldDate);
print("Difference: " + difference.TotalSeconds);
timex = timex + difference.TotalSeconds;
}
function OnApplicationQuit()
{
//Save the current system time as a string in the playerprefs class
PlayerPrefs.SetString("Time_Data", System.DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs: " + System.DateTime.Now);
}
What is weird about this code is before it was working on my android device but when i tried to make another app it does not work anymore, i dont know why this happens..
Why do you still use javascript (unityscript)? Unity no longer supports this, and uses C#.
https://blogs.unity3d.com/2017/08/11/unityscripts-long-ride-off-into-the-sunset/
because im not very good at csharp, but before it was working well on my previous one the same code and when i create another one it has this formatting exception, i can build the project but it does nothing in android, i have googled some but i think this is a DateTime.now issue, but how come my other previous app works and now this does not work... thanks..
Answer by unity_MXGApNcapNfNLg · Sep 02, 2020 at 10:50 AM
@andyblueAMS001 A while ago i was testing with something like this so I am copy pasting this code as it is. SO you may understand it more precisely.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System;
public class TimeTest : MonoBehaviour { string a; DateTime da,da1; int hours, min, sec; int hoursNow, minNow, secNow; long secondsBefore, secondsNow,timePassed; int temp;
void Start()
{
temp = PlayerPrefs.GetInt("rated");
Debug.Log("rated" + temp);
//Debug.Log(" Time: "+System.DateTime.Now.ToString("HH:mm:ss"));
da = System.DateTime.Now;
PlayerPrefs.SetInt("Hours", int.Parse(da.ToString("HH")));
PlayerPrefs.SetInt("Minutes", int.Parse(da.ToString("mm")));
PlayerPrefs.SetInt("Seconds", int.Parse(da.ToString("ss")));
hours=PlayerPrefs.GetInt("Hours");
min=PlayerPrefs.GetInt("Minutes");
sec=PlayerPrefs.GetInt("Seconds");
//Debug.Log(" time " + hours+" "+ min+" " + sec);
StartCoroutine(ChangeTime());
}
IEnumerator ChangeTime()
{
yield return new WaitForSeconds(10.0f);
hours = PlayerPrefs.GetInt("Hours");
min = PlayerPrefs.GetInt("Minutes");
sec = PlayerPrefs.GetInt("Seconds");
da1 = System.DateTime.Now;
secondsBefore = (hours * 60 * 60) + (min * 60) + sec;
hoursNow = int.Parse(da1.ToString("HH"));
minNow= int.Parse(da1.ToString("mm"));
secNow= int.Parse(da1.ToString("ss"));
secondsNow= (hoursNow * 60 * 60) + (minNow * 60) + secNow;
timePassed = secondsNow - secondsBefore;
Debug.Log("Ti9mePassed " + timePassed);
}
Your answer
Follow this Question
Related Questions
How to compare two dates? 1 Answer
calendar gui for date selection 4 Answers
playerprefs save date cheat problem 1 Answer
How to Save Object (Button) information on game restarting? please check code. 2 Answers
How can I save my game data 1 Answer