C# 時間函數 DateTime.Compare(datetime1, datetime2)用法
C# 時間函數 DateTime.Compare(datetime1, datetime2)用法
using System;
class Program
{
static void Main()
{
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0); // 8月1日午夜
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0); // 同一天的中午
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than"; //date1 < date2
else if (result == 0)
relationship = "is the same time as"; //date1 == date2
else
relationship = "is later than"; //date1 > date2
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
}
}
留言
張貼留言