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() { } }
2.可以直接判断时间大小
public bool boolMain(string timeString,string localDateTime) { // 设置东八区 //var chinaZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"); var chinaZone = DateTime.Now.ToLocalTime(); // 使用DateTime.Parse转换字符串到DateTime DateTime dateTime = DateTime.Parse(timeString); // 转换为东八区时间 //DateTime GetJsonTime = TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, chinaZone); // 使用DateTime.Parse转换字符串到DateTime DateTime localTime = DateTime.Parse(localDateTime); // 转换为东八区时间 //DateTime GetLocalTime = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Local, chinaZone); Debug.Log("dateTime:"+ dateTime+ "localTime"+ localTime); // 判断固定时间是否在本地时间之后 //if (GetLocalTime == GetJsonTime) if (dateTime == localTime) { //Console.WriteLine("固定时间点在当前时间之后。"); return false; } else { //Console.WriteLine("固定时间点在当前时间之前或相同。"); return true; } //return false }

浙公网安备 33010602011771号