|
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 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!!
(comments are locked)
|
|
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! This has solved by problem, thank you very much!!
Sep 28 '11 at 03:16 PM
HeyItsDaijoubu
(comments are locked)
|
|
Add From there, you can start easily using DateTime objects to store, restore, and compare dates. For instance:
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 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 MSDN and such to be done for C# :D
Sep 28 '11 at 03:18 PM
HeyItsDaijoubu
(comments are locked)
|
