DateTime.MaxValue 字段

定义

表示 DateTime 的最大可能值。 此字段为只读。

public: static initonly DateTime MaxValue;
public static readonly DateTime MaxValue;
 staticval mutable MaxValue : DateTime
Public Shared ReadOnly MaxValue As DateTime 

字段值

示例

下面的示例通过向其构造函数传递表示Int64一个时钟周期数的值来实例化DateTime对象。 在调用构造函数之前,该示例确保此值大于或等于 DateTime.MinValue.Ticks 且小于或等于 DateTime.MaxValue.Ticks。 否则,它将引发 ArgumentOutOfRangeException

// Attempt to assign an out-of-range value to a DateTime constructor.
long numberOfTicks = Int64.MaxValue;
DateTime validDate;

// Validate the value.
if (numberOfTicks >= DateTime.MinValue.Ticks &&
    numberOfTicks <= DateTime.MaxValue.Ticks)
   validDate = new DateTime(numberOfTicks);
else if (numberOfTicks < DateTime.MinValue.Ticks)
   Console.WriteLine("{0:N0} is less than {1:N0} ticks.",
                     numberOfTicks,
                     DateTime.MinValue.Ticks);
else
   Console.WriteLine("{0:N0} is greater than {1:N0} ticks.",
                     numberOfTicks,
                     DateTime.MaxValue.Ticks);
// The example displays the following output:
//   9,223,372,036,854,775,807 is greater than 3,155,378,975,999,999,999 ticks.
// Attempt to assign an out-of-range value to a DateTime constructor.
let numberOfTicks = Int64.MaxValue

// Validate the value.
if numberOfTicks >= DateTime.MinValue.Ticks &&
   numberOfTicks <= DateTime.MaxValue.Ticks then
    let validDate = DateTime numberOfTicks
    ()
elif numberOfTicks < DateTime.MinValue.Ticks then
    printfn $"{numberOfTicks:N0} is less than {DateTime.MinValue.Ticks:N0} ticks."
else
    printfn $"{numberOfTicks:N0} is greater than {DateTime.MaxValue.Ticks:N0} ticks."
// The example displays the following output:
//   9,223,372,036,854,775,807 is greater than 3,155,378,975,999,999,999 ticks.
' Attempt to assign an out-of-range value to a DateTime constructor.
Dim numberOfTicks As Long = Int64.MaxValue
Dim validDate As Date

' Validate the value.
If numberOfTicks >= Date.MinValue.Ticks And _
   numberOfTicks <= Date.MaxValue.Ticks Then
   validDate = New Date(numberOfTicks)
ElseIf numberOfTicks < Date.MinValue.Ticks Then
   Console.WriteLine("{0:N0} is less than {1:N0} ticks.", 
                     numberOfTicks, 
                     DateTime.MinValue.Ticks)      
Else                                                   
   Console.WriteLine("{0:N0} is greater than {1:N0} ticks.", 
                     numberOfTicks, 
                     DateTime.MaxValue.Ticks)     
End If
' The example displays the following output:
'   9,223,372,036,854,775,807 is greater than 3,155,378,975,999,999,999 ticks.

注解

此常量的值相当于公历 9999 年 12 月 31 日 23:59:59.9999999 UTC,恰好是 10000 年 1 月 1 日 00:00:00 UTC 之前的一个 100 纳秒的刻度。

某些日历(如 ThaiBuddhistCalendar)支持早于 MaxValue的日期范围。 在这些情况下,尝试在变量赋值或格式设置和分析操作中访问 MaxValue 可能会引发 ArgumentOutOfRangeException。 可以从 属性中检索指定区域性的最新有效日期值Calendar.MaxSupportedDateTime的值,而不是检索 DateTime.MaxValue的值。

适用于

另请参阅