Share via


DateTime.CompareTo メソッド

指定したオブジェクトとこのインスタンスを比較し、これらの相対値を示す値を返します。

Public Overridable Function CompareTo( _
   ByVal value As Object _) As Integer
[C#]
public virtual int CompareTo(objectvalue);
[C++]
public: virtual int CompareTo(Object* value);
[JScript]
public function CompareTo(
   value : Object) : int;

パラメータ

  • value
    比較対象のオブジェクト、または null 参照 (Visual Basic では Nothing) 。

戻り値

このインスタンスと value の相対値を示す符号付き数値。

説明
0 より小さい値 このインスタンスは value よりも小さくなっています。
0 このインスタンスは value と等価です。
0 より大きい値 このインスタンスは value より大きいか、または value が null 参照 (Visual Basic では Nothing) です。

例外

例外の種類 条件
ArgumentException value が DateTime ではありません。

解説

DateTime のすべてのインスタンスは、値に関係なく、 null 参照 (Visual Basic では Nothing) より大きいと見なされます。

value が DateTime のインスタンスであるか、または null 参照 (Nothing) である必要があります。それ以外の場合は例外がスローされます。

使用例

[Visual Basic, C#, C++] CompareTo メソッドを次のサンプルで示します。

 
Dim thDay As New System.DateTime(System.DateTime.Today.Year, 7, 28)

Dim compareValue As Integer
Try
   compareValue = thDay.CompareTo(System.DateTime.Today)
Catch exp As ArgumentException
   System.Console.WriteLine("Value is not a DateTime")
End Try

If compareValue < 0 Then
   System.Console.WriteLine("{0:d} is in the past.", thDay)
ElseIf compareValue = 0 Then
   System.Console.WriteLine("{0:d} is today!", thDay)
ElseIf compareValue = 1 Then
   System.Console.WriteLine("Value is null")
Else ' compareValue > 0 && compareValue != 1
   System.Console.WriteLine("{0:d} has not come yet.", thDay)
End If

[C#] 
System.DateTime theDay = 
          new System.DateTime(System.DateTime.Today.Year, 7, 28);

int compareValue;
try {
    compareValue = theDay.CompareTo(System.DateTime.Today);
} 
catch (ArgumentException) {
    System.Console.WriteLine("Value is not a DateTime");
    return;
}

if (compareValue < 0) {
    System.Console.WriteLine("{0:d} is in the past.", theDay);
} 
else if (compareValue == 0) {
    System.Console.WriteLine("{0:d} is today!", theDay);
}
else if (compareValue == 1) {
    System.Console.WriteLine("Value is null");
}
// compareValue > 0 && compareValue != 1
else { 
    System.Console.WriteLine("{0:d} has not come yet.", theDay);
}

[C++] 
System::DateTime theDay(System::DateTime::Today.Year, 7, 28);

int compareValue;
try {
   compareValue = theDay.CompareTo(__box(System::DateTime::Today));
} catch (ArgumentException*) {
   System::Console::WriteLine(S"Value is not a DateTime");
   return;
}

if (compareValue < 0) {
   System::Console::WriteLine(S"{0:d} is in the past.", __box(theDay));
} else if (compareValue == 0) {
   System::Console::WriteLine(S"{0:d} is today!", __box(theDay));
} else if (compareValue == 1) {
   System::Console::WriteLine(S"Value is 0");
}
// compareValue > 0 && compareValue != 1
else {
   System::Console::WriteLine(S"{0:d} has not come yet.", __box(theDay));
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

DateTime 構造体 | DateTime メンバ | System 名前空間 | Compare | Equals