当前位置:   article > 正文

Unity-DateTime显示当前时间和获取时间戳_unity 获取当前时间戳

unity 获取当前时间戳

1 显示当前时间,显示的格式为20220506-11:19:30
2 输出单位为秒的时间戳
3 输出单位为毫秒的时间戳

//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;
        }
    }



}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/118933?site
推荐阅读
相关标签
  

闽ICP备14008679号