Unity 比較時間大小


1.比較時間的大小

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Time_Size : MonoBehaviour
{
    public string msg;
    private void Start()
    {
        DateTime date = DateTime.Now.ToLocalTime();  //當前系統時間
        string data1 = "2021/1/1 12:12:12";       // "/" 跟 "-" 效果是一樣的
        //string data3 = "2021-01-01 12:12:12";
        CompanyDate(date .ToString (), data1, ref msg);
    }
    public void CompanyDate(string dateStr1, string dateStr2, ref string msg)
    {
        //將日期字符串轉換為日期對象
        DateTime t1 = Convert.ToDateTime(dateStr1);
        DateTime t2 = Convert.ToDateTime(dateStr2);
        //通過DateTIme.Compare()進行比較()
        int num = DateTime.Compare(t1, t2);

        //t1> t2
        if (num > 0)
        {
            msg = "t1:(" + dateStr1 + ")大於" + "t2(" + dateStr2 + ")";
        }
        //t1= t2
        if (num == 0)
        {
            msg = "t1:(" + dateStr1 + ")等於" + "t2(" + dateStr2 + ")";
        }
        //t1< t2
        if (num < 0)
        {
            msg = "t1:(" + dateStr1 + ")小於" + "t2(" + dateStr2 + ")";
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM