欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity C#把指定时间转化成时间戳

程序员文章站 2022-06-15 23:10:32
今天又学到一种 用C#把指定时间转化成时间戳比如今天的晚上八点DateTime today = new DateTime(now.Year, now.Month, now.Day, 20, 0, 0);System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));//这是历史最早的时间戳 long t = (today.Ticks...

今天又学到一种 用C#把指定时间转化成时间戳

比如今天的晚上八点

DateTime today = new DateTime(now.Year, now.Month, now.Day, 20, 0, 0);
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));//这是历史最早的时间戳

        long t = (today.Ticks - startTime.Ticks) / 10000000;//得到了那个点的时间戳(这里用long好像会丢精度   建议decimal)

decimal unix = (decimal)(today - DateTime.Now ).TotalSeconds; // 现在时间到那个点的相差秒数

这下面 做笔记的 可以忽略
int hookTimeS = (int)unix / 3600;
decimal hookTime1 = unix / 60;
decimal hookTime2 = hookTimeS * 60;
decimal hookTimeF = hookTime1 - hookTime2;
decimal hookTimeM = hookTimeF * 60 - (int)hookTimeF * 60;
Debug.Log($"{hookTimeS}时,{(int)hookTimeF}分,{(int)hookTimeM}秒");
TimeS = hookTimeS; TimeF = (int)hookTimeF; TimeM = (int)hookTimeM;

本文地址:https://blog.csdn.net/qq_35864875/article/details/107659034

相关标签: unity unix c#