unity C# 时间换算记录

1s = 10000000ticks 奈秒    一千万奈秒

换算起始记录时间 1970年1月1日8点整

long ———— dateTime
// long --> DateTime
    public static DateTime ConvertLongToDateTime(long d)
    {
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(d + "0000");
        TimeSpan toNow = new TimeSpan(lTime);
        DateTime dtResult = dtStart.Add(toNow);
        return dtResult;
    }

DateTime————long
// DateTime --> long
    public static long ConvertDataTimeToLong(DateTime dt)
    {
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        TimeSpan toNow = dt.Subtract(dtStart);
        long timeStamp = toNow.Ticks;
        timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
        return timeStamp;
    }

Int————string 显示时分秒
//Int数值转成string时间显示
    public static string FormatIntToStringDate(int time)
    {
        int days = time / (3600 * 24);
        int hours = (time / 3600) - (days * 24);
        int minutes = (time / 60) - (hours * 60) - (days * 60 * 24);
        int seconds = time - minutes * 60 - hours * 3600 - days * 3600 * 24;

        string showTime = "";
        if (days == 0)
        {
            showTime = string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
        }
        else
        {
            showTime = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", days, hours, minutes, seconds);
        }

        return showTime;
    }

猜你喜欢

转载自blog.csdn.net/jiangyangll/article/details/79723039
今日推荐