C#把时间秒数转化成时间格式字符串00:00:00

///
/// 把时间秒数转化成字符串00:00:00
///

///时间秒数 /// 字符串00:00:00

    public static string TransTimeSecondIntToString(int second)
    {
        string str = "";
        try
        {
            int hour = second / 3600;
            int min = second % 3600 / 60;
            int sec = second % 60;
            if (hour < 10)
            {
                str += "0" + hour.ToString();
            }
            else
            {
                str += hour.ToString();
            }
            str += ":";
            if (min < 10)
            {
                str += "0" + min.ToString();
            }
            else
            {
                str += min.ToString();
            }
            str += ":";
            if (sec < 10)
            {
                str += "0" + sec.ToString();
            }
            else
            {
                str += sec.ToString();
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogWarning("Catch:" + ex.Message);
        }
        return str;
    }
    /// <summary>
    /// 把时间秒数转化成字符串00:00:00
    /// </summary>
    /// <param name="second">时间秒数</param>
    /// <returns>字符串00:00:00</returns>
    public static string TransTimeSecondIntToString(long second)
    {
        string str = "";
        try
        {
            long hour = second / 3600;
            long min = second % 3600 / 60;
            long sec = second % 60;
            if (hour < 10)
            {
                str += "0" + hour.ToString();
            }
            else
            {
                str += hour.ToString();
            }
            str += ":";
            if (min < 10)
            {
                str += "0" + min.ToString();
            }
            else
            {
                str += min.ToString();
            }
            str += ":";
            if (sec < 10)
            {
                str += "0" + sec.ToString();
            }
            else
            {
                str += sec.ToString();
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogWarning("Catch:" + ex.Message);
        }
        return str;
    }
    /// <summary>
    /// 将时间格式(00:00:00)转换为秒
    /// </summary>
    /// <param name="time"> 时间格式字符串</param>
    /// <returns> 秒数</returns>
    public static int ConvertTimeToSecond(string time)
    {
        string[] str = time.Split(':');
        int h = 0;
        int m = 0;
        int s = 0;
        if (!int.TryParse(str[0],out h))
        {
        }
        if (!int.TryParse(str[1], out m))
        {
        }
        if (!int.TryParse(str[2], out s))
        {
        }
        h = h * 3600;
        m = m * 60;
        s = h + m + s;
        return s;
    }

 

本文链接:

https://bobsong.cn/141.html
1 + 7 =
快来做第一个评论的人吧~