时间比较方法DateTime.Compare

格式:DateTime.Compare(datetime1, datetime2)

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

 

实例:

 1 string st1 = "12:13";
 2 string st2 = "14:13";
 3 DateTime t1 = Convert.ToDateTime(st1);
 4 Console.WriteLine(t1);  //20186/6 下午 12:12:00
 5 DateTime t2 = Convert.ToDateTime(st2);
 6 Console.WriteLine(t2);  //20186/6 下午 02:14:00
 7 DateTime t3 = DateTime.Now;
 8 Console.WriteLine(t3);
 9 if (DateTime.Compare(t1, t2) > 0) Console.WriteLine(st1+">"+st2);
10 else if (DateTime.Compare(t1, t2) == 0) Console.WriteLine(st1+"="+st2);
11 else Console.WriteLine(st1+"<"+st2);
12 
13 if (DateTime.Compare(t1, t3) > 0) Console.WriteLine("\r\n"+st1 + ">" + t3.ToString());
14 else if (DateTime.Compare(t1, t3) == 0) Console.WriteLine("\r\n" + st1 + "=" + t3.ToString());
15 else Console.WriteLine("\r\n" + st1 + "<" + t3.ToString());

 

posted on 2018-06-06 09:39  空空Ryan  阅读(7152)  评论(0编辑  收藏  举报

导航