【问题标题】:How do I go about turning the time into a public int?我该如何将时间变成公共整数?
【发布时间】:2015-06-30 23:27:48
【问题描述】:

如何将时间转换为公共 int,我需要将 TimeSpan 差异转换为秒,然后将其保存为公共 int。

在 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);
    }




    }

【问题讨论】:

  • 只保存为日期时间有什么问题?
  • 我有点想用它来与其他整数相乘
  • 你是对的...thanx

标签: c# datetime time unity3d int


【解决方案1】:

您可以使用 TotalSeconds 属性:

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

【讨论】:

  • .TotalSeconds 是完美的。现在我需要弄清楚的是如何设置差异。 Totalseconds 到一个整数
【解决方案2】:

问题解决了!谢谢大家!

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);
}   
}

【讨论】:

    猜你喜欢
    • 2019-11-18
    • 2021-04-02
    • 2010-10-05
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    相关资源
    最近更新 更多