C# 获取时间差

版权声明:欢迎加入qq交流群:956187480 https://blog.csdn.net/qq_37310110/article/details/88964457
public class Controller : MonoBehaviour {

    int timeLimit = 1;//限制时间
    DateTime runTime;//启动时间
    DateTime currentTime = DateTime.Now;//当前时间
    string path = "";
    void Start()
    {
        path = Application.streamingAssetsPath + "/RunTime";
        if (!File.Exists(path))
        {
            //获取当前启动时间
            Debug.Log("第一次启动时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //存入启动时间
            File.WriteAllBytes(path, Encoding.UTF8.GetBytes(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
        //读取第一次启动时间
        runTime = Convert.ToDateTime(File.ReadAllLines(path)[0]);
        Debug.Log("第一次启动时间:" + runTime.ToString("yyyy - MM - dd HH: mm:ss"));


        TimeSpan ts1 = new TimeSpan(runTime.Ticks);

        TimeSpan ts2 = new TimeSpan(currentTime.Ticks);

        TimeSpan tsSub = ts1.Subtract(ts2).Duration();

        Debug.Log("天/" + tsSub.Days + "-时/" + tsSub.Hours + "-分/" + tsSub.Minutes);
        if (tsSub.Days >= timeLimit)
        {
            //天数
            Debug.Log("退出");
        }
        if (tsSub.Hours >= timeLimit)
        {
            //小时数
            Debug.Log("退出");
        }
        if (tsSub.Minutes >= timeLimit)
        {
            //分钟数
            Debug.Log("退出");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37310110/article/details/88964457