C# DateTime MaxValue

Description

DateTime MaxValue represents the largest possible value of DateTime. This field is read-only.

Syntax

DateTime.MaxValue has the following syntax.


public static readonly DateTime MaxValue

Example

The following example shows how to use the largest possible value of DateTime.


using System;/*  w ww.  jav a  2  s .com*/
public class MainClass{
  public static void Main(String[] argv){  

    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) 
       System.Console.WriteLine("{0:N0} is less than {1:N0} ticks.", 
                         numberOfTicks, 
                         DateTime.MinValue.Ticks);      
    else
       System.Console.WriteLine("{0:N0} is greater than {1:N0} ticks.", 
                         numberOfTicks,
                         DateTime.MaxValue.Ticks);
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version