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

这篇博客介绍了如何在Unity中将时间转换为小时:分钟:秒的格式。通过创建一个TimeSpan对象并利用其属性,可以轻松地格式化秒数为HH:mm:ss的字符串。代码示例展示了如何在Unity中实现这一转换,并提供了转换函数FormatTime的详细实现。

原地址:(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;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值