C# 我该如何把时间变成公共时间?

C# 我该如何把时间变成公共时间?,c#,datetime,time,unity3d,int,C#,Datetime,Time,Unity3d,Int,如何将时间转换为公共整数,我需要将时间跨度差转换为秒,然后将其保存为公共整数 在4小时22分34秒的时间里变成了15754 using UnityEngine; using System.Collections; using System; public class Timeload : MonoBehaviour { DateTime currentDate; DateTime oldDate; void

如何将时间转换为公共整数,我需要将时间跨度差转换为秒,然后将其保存为公共整数

在4小时22分34秒的时间里变成了15754

    using UnityEngine;
    using System.Collections;
    using System;

    public class Timeload : 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);
    }




    }

您可以使用TotalSeconds属性:

//Use the Subtract method and store the result as a timespan variable
            TimeSpan difference = currentDate.Subtract(oldDate);
            print("Difference: " + difference.TotalSeconds);

问题解决了!谢谢大家

using UnityEngine;
using System.Collections;
using System;

public class Timeload : MonoBehaviour {

DateTime  currentDate;
DateTime  oldDate;

public double timex;

void Start()
{
    //Store the current time when it starts
    currentDate = System.DateTime.Now;

    //Grab the old time from the playerprefs 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.TotalSeconds);

    timex = timex + difference.TotalSeconds;
}

void OnApplicationQuit()
{
    //Save the current system time as a string in the playerprefs class
    PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());

    print("Saving this date to prefs: " + System.DateTime.Now);
}   
}

把它保存为日期时间有什么不对?我想用它和其他整数相乘你是对的。。。thanx.TotalSeconds是完美的。现在我需要弄清楚的是如何设置差异。将总秒数转换为整数