kingBook

导航

C# TimeSpan、DateTime

using System;
using UnityEngine;

TimeSpan timeSpan=TimeSpan.FromSeconds(10000);//10000秒
Debug.LogFormat("{0} {1} {2}",timeSpan.Hours,timeSpan.Minutes,timeSpan.Seconds);//output: 2 46 40
Debug.Log(timeSpan.ToString());//output: 02:46:40
Debug.Log(timeSpan.ToString(@"hh\:mm\:ss"));//output: 02:46:40
Debug.Log(timeSpan.ToString(@"mm\:ss"));//output: 46:40

//加减时间
timeSpan=timeSpan.Add(TimeSpan.FromSeconds(1));
Debug.Log(timeSpan.ToString(@"mm\:ss"));//output: 46:41
timeSpan=timeSpan.Add(TimeSpan.FromSeconds(-2));
Debug.Log(timeSpan.ToString(@"mm\:ss"));//output: 46:39
// 24 小时制
Debug.Log(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); // output: 2021-06-28 15:10:25
// 12 小时制
Debug.Log(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); // output: 2021-06-28 03:10:25

// 24 小时制
Debug.Log(System.DateTime.Now.ToString("HH:mm:ss")); // output: 15:10:25
// 12 小时制
Debug.Log(System.DateTime.Now.ToString("hh:mm:ss")); // output: 03:10:25

更多字符串的格式

posted on 2020-10-22 12:12  kingBook  阅读(232)  评论(0编辑  收藏  举报