Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

02-28 阅读 0评论

Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

  • 介绍
    • 时间戳是什么
    • 什么时候用时间戳
    • 获取时间
      • 获取当前时间
      • 获取时间戳
      • 日期转时间戳
      • 时间戳转日期
      • 将时间戳转换为多久之前
      • 星期
      • 自定义格式时间
      • 总结

        介绍

        这里附带一个时间戳和时间转换的网址

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间,Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,服务器,功能,网址,第1张
        (图片来源网络,侵删)

        时间戳是什么

        时间戳就是从1970年1月1日0时0分0秒起到现在的总毫秒数,为什么时1970/1/1/00:00:00,因为第一台计算机发明时间是这个时间,所以时间戳诞生了。

        什么时候用时间戳

        比如说你要做一些时间相关的功能,那么基本都会用到时间戳。而且时间戳是精确的,比如说要做计时、宝箱倒计时、账号禁言、封号等相关问题你请求服务器的数据一般都是通过时间戳来获取具体时间。而且如果与服务器通讯也可以通过时间戳来做一个请求响应时间。

        获取时间

        获取当前时间

        //北京时间
        DateTime date1 = DateTime.Now;
        Debug.LogError("北京时间:" + date1);
        //国际时间
        DateTime date2 = DateTime.UtcNow;
        Debug.LogError("国际时间:" + date2);
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        获取时间戳

        //时间戳方法一
        long now1 = DateTime.UtcNow.Ticks;
        Debug.LogError("当前时间戳:" + now1);
        //时间戳方法二
        long now2 = DateTime.Now.ToUniversalTime().Ticks;
        Debug.LogError("当前时间戳:" + now2);
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        日期转时间戳

        //方法一
        TimeSpan st1 = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
        Debug.LogError("日期转为毫秒时间戳:" + Convert.ToInt64(st1.TotalMilliseconds));
        Debug.LogError("日期转为秒时间戳:" + Convert.ToInt64(st1.TotalSeconds));
        Debug.LogError("日期转为分钟时间戳:" + Convert.ToInt64(st1.TotalMinutes));
        Debug.LogError("日期转为小时时间戳:" + Convert.ToInt64(st1.TotalHours));
        Debug.LogError("日期转为天时间戳:" + Convert.ToInt64(st1.TotalDays));
        //方法二
        TimeSpan st2 = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
        Debug.LogError("日期转为毫秒时间戳:" + Convert.ToInt64(st2.TotalMilliseconds));
        Debug.LogError("日期转为秒时间戳:" + Convert.ToInt64(st2.TotalSeconds));
        Debug.LogError("日期转为分钟时间戳:" + Convert.ToInt64(st2.TotalMinutes));
        Debug.LogError("日期转为小时时间戳:" + Convert.ToInt64(st2.TotalHours));
        Debug.LogError("日期转为天时间戳:" + Convert.ToInt64(st2.TotalDays));
        //方法三
        double tS1 = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
        Debug.LogError("日期转为时间戳:" + tS1);
        //方法四
        double tS2 = ((DateTime.UtcNow.Ticks - 621355968000000000) / 10000);
        Debug.LogError("日期转为时间戳:" + tS2);
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        时间戳转日期

                //方法一
                DateTime startTime1 = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
                DateTime dt1 = startTime1.AddMilliseconds(1698766775000);//传入的时间戳
                Debug.LogError("时间戳转时间:" + dt1);
                //方法二
                DateTime startDateTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1, 0, 0, 0), TimeZoneInfo.Local);
                long targetTimeStamp = ((long)1698766775000 * 10000);
                TimeSpan targetTS = new TimeSpan(targetTimeStamp);
                DateTime targetDateTime = startDateTime.Add(targetTS);
                Debug.LogError("时间戳转时间:" + targetDateTime);
                //方法三
                DateTime startTime3 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                DateTime dt3 = startTime3.AddTicks(1698766775000 * 10000);//传入的时间戳
                Debug.LogError("时间戳转时间:" + dt3);
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间,Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,服务器,功能,网址,第6张
        (图片来源网络,侵删)

        将时间戳转换为多久之前

        	void Start()
        	{
            	Debug.LogError(GetTimeLongAgo(20));
            	Debug.LogError(GetTimeLongAgo(3000));
            	Debug.LogError(GetTimeLongAgo(50000));
            	Debug.LogError(GetTimeLongAgo(864000));
            	Debug.LogError(GetTimeLongAgo(25920000));
            	Debug.LogError(GetTimeLongAgo(61104000));
            
        	}
        	/// 
            /// 将秒数时间戳转换为多久之前。传入时间戳t(t= 当前时间戳() - 指定时间的时间戳 )
            /// 
            /// 
            /// 
            public string GetTimeLongAgo(double t)
            {
                string str = "";
                double num;
                if (t = 60 && t = 3600 && t  86400 && t  2592000 && t  
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        星期

                //方案一
                string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
                string week = Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString();
                Debug.LogError(week);
                //方案二
                Debug.LogError(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek));
                //方案三
                Debug.LogError(DateTime.Today.DayOfWeek.ToString());
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        自定义格式时间

        DateTime dateTime = DateTime.Now;
        string strNowTime = string.Format("{0:D}-{1:D}-{2:D}-{3:D}-{4:D}-{5:D}-{6:D}", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
        Debug.LogError(strNowTime);
        

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间

        总结

        这里其实还有很多中方式,上面知识其中的一些,基本是够用了,欢迎评论区补充

        Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间,Unity中获取时间戳、日期、时间、毫秒、秒以相互转换、自定义格式时间,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,服务器,功能,网址,第10张
        (图片来源网络,侵删)

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]