unity 时间戳 相关用法

0.webGL平台时间转换

 DateTime now = DateTime.Now.AddMinutes(-15);
        string formattedTime = now.ToString("yyyy 年 M 月 d 日 HH:mm:ss");
        text_StartTime.text = formattedTime;

1.格式化当前时间

 private string GetTime01()
    {
        var hour = DateTime.Now.Hour;
        var minute = DateTime.Now.Minute;
        var second = DateTime.Now.Second;
        var year = DateTime.Now.Year;
        var month = DateTime.Now.Month;
        var day = DateTime.Now.Day;

        //格式化显示当前时间
        return string.Format("{3:D2}-{4:D2}-{5:D2} " + "{0:D2}:{1:D2}:{2:D2}", hour, minute, second, year, month, day);
    }



/// 
    /// 时间戳转为C#格式时间
    /// TimeTool.GetTime(TimeTool.ConvertDateTiemp(DateTime.Now).ToString())
    /// 
    /// 时间戳
    /// 
    public static DateTime GetTime(string timeStamp)
    {
        if (timeStamp.Length > 10)
        {
            timeStamp = timeStamp.Substring(0, 10);
        }
        DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(timeStamp + "0000000");
        TimeSpan toNow = new TimeSpan(lTime);
        return dateTimeStart.Add(toNow);
    }

2.获取时间戳

 /// 
    /// 获得时间戳 
    /// 
    /// 
    private DateTime GetTime()
    {
        DateTime curtimer = DateTime.Now;
        return curtimer;
    }

3.

 /// 
    /// 获得毫秒
    /// 
    /// 
    private string GetTime()
    {
        //精确到毫秒
         return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString();
      
        return curtimer;
    }

/// 
    /// 获取时间戳(精确到秒)
    /// TimeTool.ConvertDateTimep(DateTime.Now)
    /// 
    /// 时间
    public static long ConvertDateTimep(DateTime time)
    {
        return ((time.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
        //等价于:
        //return ((time.ToUniversalTime().Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000000) * 1000;
    }

4.计算当前时间戳与目标时间戳的时间间隔

public string DateStringFromNow(string dt)
    {
        string timeStamp = dt;
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(timeStamp + "0000000");
        TimeSpan toNow = new TimeSpan(lTime);
        DateTime dtResult = dtStart.Add(toNow);
        TimeSpan span = DateTime.Now - dtResult;
        if (span.TotalDays > 90)
        {
            return "3个月前";

        }
        else
        if (span.TotalDays > 60)
        {
            return "2个月前";

        }
        else if (span.TotalDays > 30)
        {

            return "1个月前";
        }
        else if (span.TotalDays > 14)
        {
            return
              "2周前";
        }
        else if (span.TotalDays > 7)
        {
            return "1周前";
        }

        else if (span.TotalDays > 1)
        {
            return string.Format("{0}天前",
                       (int)Math.Floor(span.TotalDays));
        }
        else if (span.TotalHours > 1)
        {
            return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
        }
        else if (span.TotalMinutes > 1)
        {
            return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
        }
        else if (span.TotalSeconds >= 1)
        {
            return string.Format("{0}秒前",
                       (int)Math.Floor(span.TotalSeconds));
        }


        else
        {
            return "1秒前";

        }
    }

5.计算两个时间的时间差

/// 
    /// 获取间隔秒数
    /// 
    /// 
    /// 
    /// 
    public int GetSubSeconds(DateTime startTimer, DateTime endTimer)
    {
        TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

        TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

        TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

        //返回间隔秒数(不算差的分钟和小时等,仅返回秒与秒之间的差)
        //return subTimer.Seconds;

        //返回相差时长(算上分、时的差值,返回相差的总秒数)
        return (int)subTimer.TotalSeconds;
    }

    /// 
    /// 获取两个时间的相差多少分钟
    /// 
    /// 
    /// 
    /// 
    public int GetSubMinutes(DateTime startTimer, DateTime endTimer)
    {
        TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

        TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

        TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

       // 返回相差时长(仅返回相差的分钟数)
        //return subTimer.Minutes;
        //返回相差时长(仅返回相差的总分钟数)
        return (int)subTimer.TotalMinutes;
    }


    /// 
    /// 获取两个时间的相差多少小时
    /// 
    /// 
    /// 
    /// 
    public int GetSubHours(DateTime startTimer, DateTime endTimer)
    {
        TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

        TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

        TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

        //返回相差时长(仅返回相差的小时)
        //return subTimer.Hours;
        //返回相差时长(返回相差的总小时数)
        return (int)subTimer.TotalHours;
    }

    /// 
    /// 获取两个时间的相差多少天
    /// 
    /// 
    /// 
    /// 
    public int GetSubDays(DateTime startTimer, DateTime endTimer)
    {
        TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

        TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

        TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

        //返回相差时长(仅返回相差的天数)
        //return subTimer.Days;
        //返回相差时长(返回相差的总天数)
        return (int)subTimer.TotalDays;
    }

你可能感兴趣的:(#,unity之日常开发,c#,开发语言)