时间戳以及本地时间的转化

将时间搓时间转化为零时区时间或者本地时区

 public string DisposeTime(int createTime)
    {                                                                                                                                          //utc是零时区,local是转化为本地时区
        System.DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Utc);
        System.DateTime dt = startTime.AddSeconds(createTime);
        string t = dt.ToString("yyyy.MM.dd HH:mm:ss");//转化为日期时间
        return t;
    }

获取当前时间的时间戳

public long GetUTCTime()
    {
        System.TimeSpan st = System.DateTime.UtcNow - new System.DateTime(1970, 1, 1, 0, 0, 0);//获取时间戳
        return Convert.ToInt64(st.TotalSeconds);
    }

目标时间戳和现在时间戳的时间差

 public string GetReMainTime(long create,int hour)
    {
        long scend = hour * 3600;
        long nowScend = GetUTCTime();
        long offset = create + scend - nowScend;
        if (offset <= 0)
        {
            return "";
        }
        return DisposeScend(offset);
    }

    public string DisposeScend(long scend)
    {
        return UIUtil.ToTimeThreeFormat(scend);
    }
    
    public static string ToTimeThreeFormat(float timer)
    {
            int s = (int)timer;
            int hour = s / 3600;
            int minute = s % 3600 / 60;
            int second = s % 3600 % 60;
            return string.Format("{0:D2}:{1:D2}:{2:D2}",hour,minute, second);
    }

你可能感兴趣的:(游戏开发,开发语言)