DateTime.Compare(t1,t2)比较两个日期大小

DateTime.Compare(t1,t2)比较两个日期大小,排前面的小,排在后面的大,比如:2011-2-1就小于2012-3-2
返回值小于零:  t1 小于 t2。 
返回值等于零 : t1 等于 t2。 
返回值大于零:  t1 大于 t2。 

如:

[c-sharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.   
  13.             DateTime t1 = DateTime.Parse("2011-2-2");  
  14.             DateTime t2 = DateTime.Parse("2011-3-1");  
  15.   
  16.             if (DateTime.Compare(t1, t2) > 0)   
  17.                 Console.WriteLine("t1 > t2");  
  18.             if (DateTime.Compare(t1, t2) == 0)   
  19.                 Console.WriteLine("t1 == t2");  
  20.             if (DateTime.Compare(t1, t2) < 0)   
  21.                 Console.WriteLine("t1 < t2");  
  22.         }  
  23.     }  
  24. }  

 

运行结果:

t1 < t2
请按任意键继续. . .

文章出处:https://blog.csdn.net/aspnet2002web/article/details/6171415

猜你喜欢

转载自www.cnblogs.com/net-sky/p/9236531.html