• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by andyblueAMS001 · Sep 02, 2020 at 08:00 AM · playerprefssave datadatetimeconvertingdate

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 t$$anonymous$$ngs 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 t$$anonymous$$s date to prefs: " + System.DateTime.Now);


}

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andyblueAMS001 · Sep 02, 2020 at 08:26 AM 0
Share

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..

avatar image ShadyProductions andyblueAMS001 · Sep 02, 2020 at 09:27 AM 0
Share

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/

avatar image andyblueAMS001 ShadyProductions · Sep 02, 2020 at 10:16 AM 0
Share

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..

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by unity_MXGApNcapNfNLg · Sep 02, 2020 at 10:50 AM

@andyblueAMS001 A w$$anonymous$$le ago i was testing with somet$$anonymous$$ng like t$$anonymous$$s so I am copy pasting t$$anonymous$$s 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);


 }
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to compare two dates? 1 Answer

Differences between mobile save/load data 1 Answer

Save Datasets at runtime 2 Answers

Google play services saved games highscore error 0 Answers

Making a less complicated save system. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges