本文主要介绍了C#中DateTime.Compare()比较时间大小,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
比较 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
到此这篇关于C#中DateTime.Compare()比较时间大小的文章就介绍到这了,更多相关C# DateTime.Compare()比较时间内容请搜索织梦云以前的文章希望大家以后多多支持织梦云!
沃梦达教程
本文标题为:C#中DateTime.Compare()比较时间大小
猜你喜欢
- Unity3D实现渐变颜色效果 2023-01-16
- 如何使用C# 捕获进程输出 2023-03-10
- Unity Shader实现模糊效果 2023-04-27
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- user32.dll 函数说明小结 2022-12-26
- c# 模拟线性回归的示例 2023-03-14
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- .NET CORE DI 依赖注入 2023-09-27
- Oracle中for循环的使用方法 2023-07-04