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() { } }
