Search Unity

Measuring time differnce between closing app and opening it again.

Discussion in 'Scripting' started by dijital, May 16, 2017.

  1. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232
    Hi,

    I am using the code below to track the time between my app closing and it re opening it again. It works just fine but I have no idea on how to use the output.

    How do use the 'difference' string?

    For example.

    if (difference > 2 hours)
    //do somethinfg

    I'm clueless here.

    Any help would be appreciated.

    Code (CSharp):
    1.      using UnityEngine;
    2.      using System.Collections;
    3.      using System;
    4.    
    5.      public class DataMaster : MonoBehaviour
    6.      {
    7.          DateTime currentDate;
    8.          DateTime oldDate;
    9.          void Start()
    10.          {
    11.              //Store the current time when it starts
    12.              currentDate = System.DateTime.Now;
    13.    
    14.              //Grab the old time from the player prefs as a long
    15.              long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
    16.    
    17.              //Convert the old time from binary to a DataTime variable
    18.              DateTime oldDate = DateTime.FromBinary(temp);
    19.              print("oldDate: " + oldDate);
    20.    
    21.              //Use the Subtract method and store the result as a timespan variable
    22.              TimeSpan difference = currentDate.Subtract(oldDate);
    23.              print("Difference: " + difference);
    24.    
    25.          }
    26.    
    27.          void OnApplicationQuit()
    28.          {
    29.              //Savee the current system time as a string in the player prefs class
    30.              PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
    31.    
    32.              print("Saving this date to prefs: " + System.DateTime.Now);
    33.          }
    34.    
    35.      }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    dijital likes this.
  3. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232