This simple value type represents a
moment in time from 12:00:00 A.M.,
1/1/0001 C.E. (Common Era), to 11:59:59 P.M., 12/31/9999 C.E., which
is measured to the nearest tick, or
100-nanosecond interval. You can use this type in
greater-than/less-than comparisons, sorting, and in calculations
using other DateTime or
TimeSpan instances. You can also use convenient
Add. . . methods, such as AddSeconds(
), with a positive or negative value.
To extract part of a date, use properties such as
Day and Minute. All properties
except Ticks represent a single component of a
compound date, not the whole date. You can convert a string into a
DateTime using the static Parse(
) or ParseExact( ) methods, which
require that the date match the pattern specified by a supplied
format string.
The DateTime class also provides valuable static
functions that can determine the number of days in a month
(DaysInMonth( )), evaluate whether a year is a
leap year (IsLeapYear( )), and retrieve the date
stamp from a file (FromFileTime( )). You can also
get the current date from the static property
Today (or UtcNow for the
coordinated universal time).
public struct DateTime : IComparable, IFormattable, IConvertible {
// Public Constructors
public DateTime(int year, int month, int day);
public DateTime(int year, int month, int day, System.Globalization.Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second);
public DateTime(int year, int month, int day, int hour, int minute, int second,
System.Globalization.Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second,
int millisecond);
public DateTime(int year, int month, int day, int hour, int minute, int second,
int millisecond, System.Globalization.Calendar calendar);
public DateTime(long ticks);
// Public Static Fields
public static readonly DateTime MaxValue;
// =12/31/9999 11:59:59 PM
public static readonly DateTime MinValue;
// =1/1/0001 12:00:00 AM
// Public Static Properties
public static DateTime Now{get; }
public static DateTime Today{get; }
public static DateTime UtcNow{get; }
// Public Instance Properties
public DateTime Date{get; }
public int Day{get; }
public DayOfWeek DayOfWeek{get; }
public int DayOfYear{get; }
public int Hour{get; }
public int Millisecond{get; }
public int Minute{get; }
public int Month{get; }
public int Second{get; }
public long Ticks{get; }
public TimeSpan TimeOfDay{get; }
public int Year{get; }
// Public Static Methods
public static int Compare(DateTime t1, DateTime t2);
public static int DaysInMonth(int year, int month);
public static bool Equals(DateTime t1, DateTime t2);
public static DateTime FromFileTime(long fileTime);
public static DateTime FromFileTimeUtc(long fileTime);
public static DateTime FromOADate(double d);
public static bool IsLeapYear(int year);
public static DateTime Parse(string s);
public static DateTime Parse(string s, IFormatProvider provider);
public static DateTime Parse(string s, IFormatProvider provider,
System.Globalization.DateTimeStyles styles);
public static DateTime ParseExact(string s, string[ ] formats, IFormatProvider provider,
System.Globalization.DateTimeStyles style);
public static DateTime ParseExact(string s, string format, IFormatProvider provider);
public static DateTime ParseExact(string s, string format, IFormatProvider provider,
System.Globalization.DateTimeStyles style);
public static DateTime operator -(DateTime d, TimeSpan t);
public static TimeSpan operator -(DateTime d1, DateTime d2);
public static DateTime operator +(DateTime d, TimeSpan t);
public static bool operator !=(DateTime d1, DateTime d2);
public static bool operator <(DateTime t1, DateTime t2);
public static bool operator <=(DateTime t1, DateTime t2);
public static bool operator = =(DateTime d1, DateTime d2);
public static bool operator >(DateTime t1, DateTime t2);
public static bool operator >=(DateTime t1, DateTime t2);
// Public Instance Methods
public DateTime Add(TimeSpan value);
public DateTime AddDays(double value);
public DateTime AddHours(double value);
public DateTime AddMilliseconds(double value);
public DateTime AddMinutes(double value);
public DateTime AddMonths(int months);
public DateTime AddSeconds(double value);
public DateTime AddTicks(long value);
public DateTime AddYears(int value);
public int CompareTo(object value);
// implements IComparable
public override bool Equals(object value);
// overrides ValueType
public string[ ] GetDateTimeFormats( );
public string[ ] GetDateTimeFormats(char format);
public string[ ] GetDateTimeFormats(char format, IFormatProvider provider);
public string[ ] GetDateTimeFormats(IFormatProvider provider);
public override int GetHashCode( );
// overrides ValueType
public TypeCode GetTypeCode( );
// implements IConvertible
public DateTime Subtract(TimeSpan value);
public TimeSpan Subtract(DateTime value);
public long ToFileTime( );
public long ToFileTimeUtc( );
public DateTime ToLocalTime( );
public string ToLongDateString( );
public string ToLongTimeString( );
public double ToOADate( );
public string ToShortDateString( );
public string ToShortTimeString( );
public override string ToString( );
// overrides ValueType
public string ToString(IFormatProvider provider);
// implements IConvertible
public string ToString(string format);
public string ToString(string format, IFormatProvider provider)
// implements IFormattable
public DateTime ToUniversalTime( );
}