C#中怎么使用DateTime.Compare()比较时间大小

其他教程   发布日期:2025年04月14日   浏览次数:94

这篇文章主要讲解了“C#中怎么使用DateTime.Compare()比较时间大小”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中怎么使用DateTime.Compare()比较时间大小”吧!

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

语法

  1. public static int Compare (
  2. DateTime t1,
  3. DateTime t2
  4. )

参数

t1
第一个 DateTime。

t2
第二个 DateTime。

返回值

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

值类型

条件

小于零

t1 小于 t2。

t1 等于 t2。

大于零

t1 大于 t2。

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

示例

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

运行结果:

t1 < t2

以上就是C#中怎么使用DateTime.Compare()比较时间大小的详细内容,更多关于C#中怎么使用DateTime.Compare()比较时间大小的资料请关注九品源码其它相关文章!