Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
6
Question by CrowdedWorlds · Sep 28, 2011 at 02:05 PM · c#timedatetimedifferencelong

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

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
42
Best Answer

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);
     }
 
 }
Comment
Add comment · Show 4 · 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
avatar image CrowdedWorlds · Sep 28, 2011 at 03:16 PM 0
Share

This has solved by problem, thank you very much!!

avatar image Leon700 · Apr 28, 2016 at 08:01 AM 3
Share

You can always use PlayerPrefs.Has$$anonymous$$ey("")) to check if it is the first playthrough ^^

avatar image ZOTGames · Oct 25, 2020 at 09:36 PM 0
Share

Thanks for the best resolution

avatar image TractGames · Apr 07, 2021 at 01:08 AM 0
Share

10 years later and you are still preventing mental breakdowns. Thanks!

avatar image
0

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.

Comment
Add comment · Show 1 · 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
avatar image CrowdedWorlds · Sep 28, 2011 at 03:18 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 2 · 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
avatar image Bunny83 · Aug 18, 2018 at 09:22 AM 0
Share

When you have a question, please ask a question and do not post an answer that doesn't answer the question.

avatar image Alucard-Belmont Bunny83 · Aug 18, 2018 at 04:15 PM 0
Share

Ok sorry friend

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Countdown timer into text c# 1 Answer

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


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