目录
  • 语法
  • 参数
  • 返回值
  • 示例

比较 DateTime 的两个实例,并返回它们相对值的指示。

语法

public static int Compare (
    DateTime t1,
    DateTime t2
)

参数

t1
第一个 DateTime。

t2
第二个 DateTime。

返回值

有符号数字,指示 t1 和 t2 的相对值。

值类型

条件

小于零

t1 小于 t2。

t1 等于 t2。

大于零

t1 大于 t2。

在比较 DateTime 对象之前,请确保这些对象表示的是同一时区内的时间 

示例

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。