当前位置:   article > 正文

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

unity 时间戳

介绍

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

时间戳是什么

时间戳就是从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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

获取时间戳

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

//时间戳方法二
long now2 = DateTime.Now.ToUniversalTime().Ticks;
Debug.LogError("当前时间戳:" + now2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

日期转时间戳

//方法一
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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

时间戳转日期

        //方法一
        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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

将时间戳转换为多久之前

	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));
    
	}
	/// <summary>
    /// 将秒数时间戳转换为多久之前。传入时间戳t(t= 当前时间戳() - 指定时间的时间戳 )
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public string GetTimeLongAgo(double t)
    {
        string str = "";
        double num;
        if (t < 60)
        {
            str = string.Format("{0}秒前", t);
        }
        else if (t >= 60 && t < 3600)
        {
            num = Math.Floor(t / 60);
            str = string.Format("{0}分钟前", num);
        }
        else if (t >= 3600 && t < 86400)
        {
            num = Math.Floor(t / 3600);
            str = string.Format("{0}小时前", num);
        }
        else if (t > 86400 && t < 2592000)
        {
            num = Math.Floor(t / 86400);
            str = string.Format("{0}天前", num);
        }
        else if (t > 2592000 && t < 31104000)
        {
            num = Math.Floor(t / 2592000);
            str = string.Format("{0}月前", num);
        }
        else
        {
            num = Math.Floor(t / 31104000);
            str = string.Format("{0}年前", num);
        }
        return str;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

在这里插入图片描述

星期

        //方案一
        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());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

自定义格式时间

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);
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

总结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1015344
推荐阅读
相关标签
  

闽ICP备14008679号