DateTime.Compare用法

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

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
  
            DateTime t1 = DateTime.Parse("2011-2-2");  
            DateTime t2 = DateTime.Parse("2011-3-1");  
  
            if (DateTime.Compare(t1, t2) > 0)   
                Console.WriteLine("t1 > t2");  
            if (DateTime.Compare(t1, t2) == 0)   
                Console.WriteLine("t1 == t2");  
            if (DateTime.Compare(t1, t2) < 0)   
                Console.WriteLine("t1 < t2");  
        }  
    }  
}  

运行结果:

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

posted on 2016-11-10 16:35  bosamvs  阅读(418)  评论(0编辑  收藏  举报

导航