1 显示当前时间,显示的格式为20220506-11:19:30
2 输出单位为秒的时间戳
3 输出单位为毫秒的时间戳文章来源:https://www.toymoban.com/news/detail-501711.html
//Unity-DateTime显示当前时间和获取时间戳
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
//1 显示当前时间,显示的格式为20220506-11:19:30
//2 输出单位为秒的时间戳
//3 输出单位为毫秒的时间戳
public class Demo : MonoBehaviour
{
void Start()
{
GetTime();
Debug.Log($"输出单位为秒的时间戳 = {GetTimeStampSecond()}");
Debug.Log($"输出单位为毫秒的时间戳= {GetTimeStampMilliSecond()}");
}
/// <summary>
/// 显示当前时间,显示的格式为20220506-11:09:30
/// </summary>
public void GetTime()
{
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString();
string day = DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString();
string hour = DateTime.Now.Hour < 10 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
string minute = DateTime.Now.Minute < 10 ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
string second = DateTime.Now.Second < 10 ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(year);
stringBuilder.Append(month);
stringBuilder.Append(day);
stringBuilder.Append("-");
stringBuilder.Append(hour);
stringBuilder.Append(":");
stringBuilder.Append(minute);
stringBuilder.Append(":");
stringBuilder.Append(second);
Debug.Log($"当前时间 = {stringBuilder.ToString()}");
}
/// <summary>
/// 获取时间戳-单位秒
/// </summary>
/// <returns></returns>
public long GetTimeStampSecond()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
try
{
return Convert.ToInt64(ts.TotalSeconds);
}
catch (Exception ex)
{
Debug.Log($"GetTimeStampSecond Error = {ex}");
return 0;
}
}
/// <summary>
/// 获取时间戳-单位毫秒
/// </summary>
/// <returns></returns>
public long GetTimeStampMilliSecond()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
try
{
return Convert.ToInt64(ts.TotalMilliseconds);
}
catch (Exception ex)
{
Debug.Log($"GetTimeStampMilliSecond Error = {ex}");
return 0;
}
}
}
文章来源地址https://www.toymoban.com/news/detail-501711.html
到了这里,关于Unity-DateTime显示当前时间和获取时间戳的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!