Unity将时间转换成 小时:分钟:秒 格式

原地址:(3条消息) Unity将时间转换成 小时:分钟:秒 格式_a451319296的博客-CSDN博客

记录一下:

/*******************************************************************************************************************************************************************************
                                              
                                                                            _ooOoo_      
                                                                           o8888888o
                                                                           88" . "88     
                                                                           (| -_- |)
                                                                           O\  =  /O
                                                                        ____/`---'\____
                                                                      .'  \\|     |//  `.
                                                                     /  \\|||  :  |||//  \
                                                                    /  _||||| -:- |||||-  \
                                                                    |   | \\\  -  /// |   |d
                                                                    | \_|  ''\---/''  |   |
                                                                    \  .-\__  `-`  ___/-. /
                                                                  ___`. .'  /--.--\  `. . __
                                                               ."" '<  `.___\_<|>_/___.'  >'"".
                                                              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
                                                              \  \ `-.   \_ __\ /__ _/   .-` /  /
                                                          =====`-.____`-.___\_____/___.-`____.-'======
                                                                            `=---='
                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                                                        
                                                                         \     |     /
                                                                          \    |    /
                                                                           \   |   /
                                                                            \  |  /
                                                                       \\             //
                                                                        \\___________//
                                                                        
                                                                    佛祖保佑       永无Bug
                                                                    快加工资       不改需求
                                                                            阿弥陀佛
*本模块已经经过开光处理,绝无可能再产生bug
*作者: TianWenQuan
*日期: #CreateTime#
********************************************************************************************************************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TimeZhuanHuan : MonoBehaviour
{
    public Button button;
    bool bo = true;
    float time_ = 0;
    void Start()
    {
        Debug.Log("时间="+ FormatTime(time_));
        button.onClick.AddListener(()=> {
          //  bo = false;
            Debug.Log("时间02=" + FormatTime(time_));
        });
    }

    void Update()
    {
        if (bo)
        {
            time_ += Time.deltaTime;
        }
    }
    /// <summary>
    /// 格式化时间
    /// </summary>
    /// <param name="seconds">秒</param>
    /// <returns></returns>
    public static string FormatTime(float seconds)
    {
        TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(seconds));
        string str = "";

        if (ts.Hours > 0)
        {
            str = ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
        }
        if (ts.Hours == 0 && ts.Minutes > 0)
        {
            str = "00:" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
        }
        if (ts.Hours == 0 && ts.Minutes == 0)
        {
            str = "00:" + "00:" + ts.Seconds.ToString("00");
        }

        return str;
    }
}