首页 > 其他分享 >时间比较方法DateTime.Compare

时间比较方法DateTime.Compare

时间:2023-02-22 10:39:33浏览次数:37  
标签:Compare Console t3 DateTime st1 WriteLine 方法


格式:DateTime.Compare(datetime1, datetime2)

参数为时间格式,为第一个参数比较第二个参数,返回小于0的值,等于0或大于0的值。

string st1 = "12:13";
string st2 = "14:13";
DateTime t1 = Convert.ToDateTime(st1);
Console.WriteLine(t1); //20186/6 下午 12:12:00
DateTime t2 = Convert.ToDateTime(st2);
Console.WriteLine(t2); //20186/6 下午 02:14:00
DateTime t3 = DateTime.Now;
Console.WriteLine(t3);
if (DateTime.Compare(t1, t2) > 0) Console.WriteLine(st1+">"+st2);
else if (DateTime.Compare(t1, t2) == 0) Console.WriteLine(st1+"="+st2);
else Console.WriteLine(st1+"<"+st2);

if (DateTime.Compare(t1, t3) > 0) Console.WriteLine("\r\n"+st1 + ">" + t3.ToString());
else if (DateTime.Compare(t1, t3) == 0) Console.WriteLine("\r\n" + st1 + "=" + t3.ToString());
else Console.WriteLine("\r\n" + st1 + "<" + t3.ToString());

 

标签:Compare,Console,t3,DateTime,st1,WriteLine,方法
From: https://blog.51cto.com/u_4387387/6078231

相关文章