意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

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

来源:恒创科技 编辑:恒创科技编辑部
2022-10-01 08:31:04


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



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


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


上一篇: 租用美国服务器:潜在的风险与应对策略。 下一篇: MongoDB 5.0 扩展开源文档数据库操作