您当前的位置: 首页 >  c#

幻世界

暂无认证

  • 1浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【c#编程技术总结】 获取时间差

幻世界 发布时间:2019-04-02 10:15:43 ,浏览量:1

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

public class Controller : MonoBehaviour {

    int timeLimit = 1;//限制时间
    DateTime runTime;//启动时间
    DateTime currentTime = DateTime.Now;//当前时间
    string path = "";
    void Start()
    {
        path = Application.persistentDataPath+ "/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("退出");
        }
    }
}

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

关注
打赏
1660704426
查看更多评论
立即登录/注册

微信扫码登录

0.0348s