System.DateTime Structure

Assembly: Mscorlib.dll
Namespace: System
Summary
Represents an instant in time, typically expressed as a date and time of day.
C# Syntax:
[Serializable]
public struct DateTime : IComparable, IFormattable, IConvertible
Remarks
The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 C.E. (Common Era) to 11:59:59 P.M., December 31, 9999 C.E.

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 1 C.E. in the GregorianCalendar calendar. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.

The DateTime and TimeSpan value types differ in that a DateTime represents an instant in time, whereas a TimeSpan represents a time interval. This means, for example, that you can subtract one instance of DateTime from another to obtain the time interval between them. Or you could add a positive TimeSpan to the current DateTime to calculate a future date.

Time values can be added to, or subtracted from, an instance of DateTime. Time values can be negative or positive, and expressed in units such as ticks, seconds, or instances of TimeSpan. Methods and properties in this value type take into account details such as leap years and the number of days in a month.

Descriptions of time values in this type are often expressed using the coordinated universal time (UTC) standard, which was previously known as Greenwich mean time (GMT).

Calculations and comparisons of DateTime instances are only meaningful when the instances are created in the same time zone. For that reason, it is assumed that the developer has some external mechanism, such as an explicit variable or policy, to know in which time zone a DateTime was created. Methods and properties in this class always use the local time zone when making calculations or comparisons.

A calculation on an instance of DateTime, such as DateTime.Add or DateTime.Subtract, does not modify the value of the instance. Instead, the calculation returns a new instance of DateTime whose value is the result of the calculation.

This type inherits from IComparable, IFormattable, and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.

See also:
System Namespace | TimeSpan | Calendar | TimeZone.GetUtcOffset

System.DateTime Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(long ticks)

Initializes a new instance of the DateTime structure to a specified number of ticks.
ctor #2 Overloaded:
.ctor(int year, int month, int day)

Initializes a new instance of the DateTime structure to the specified year, month, and day.
ctor #3 Overloaded:
.ctor(int year, int month, int day, Calendar calendar)

Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified Calendar.
ctor #4 Overloaded:
.ctor(int year, int month, int day, int hour, int minute, int second)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.
ctor #5 Overloaded:
.ctor(int year, int month, int day, int hour, int minute, int second, Calendar calendar)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified Calendar.
ctor #6 Overloaded:
.ctor(int year, int month, int day, int hour, int minute, int second, int millisecond)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.
ctor #7 Overloaded:
.ctor(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified Calendar.
Public Fields
MaxValue Represents the largest possible value of DateTime. This field is read-only.
MinValue Represents the smallest possible value of DateTime. This field is read-only.
Public Properties
Date Read-only

Gets the date component of this instance.
Day Read-only

Gets the day of the month represented by this instance.
DayOfWeek Read-only

Gets the day of the week represented by this instance.
DayOfYear Read-only

Gets the day of the year represented by this instance.
Hour Read-only

Gets the hour component of the date represented by this instance.
Millisecond Read-only

Gets the milliseconds component of the date represented by this instance.
Minute Read-only

Gets the minute component of the date represented by this instance.
Month Read-only

Gets the month component of the date represented by this instance.
Now Read-only

Gets a DateTime that is the current local date and time on this computer.
Second Read-only

Gets the seconds component of the date represented by this instance.
Ticks Read-only

Gets the number of ticks that represent the date and time of this instance.
TimeOfDay Read-only

Gets the time of day for this instance.
Today Read-only

Gets the current date.
UtcNow Read-only

Gets a DateTime that is the current local date and time on this computer expressed as the coordinated universal time (UTC).
Year Read-only

Gets the year component of the date represented by this instance.
Public Methods
Add Adds the value of the specified TimeSpan to the value of this instance.
AddDays Adds the specified number of days to the value of this instance.
AddHours Adds the specified number of hours to the value of this instance.
AddMilliseconds Adds the specified number of milliseconds to the value of this instance.
AddMinutes Adds the specified number of minutes to the value of this instance.
AddMonths Adds the specified number of months to the value of this instance.
AddSeconds Adds the specified number of seconds to the value of this instance.
AddTicks Adds the specified number of ticks to the value of this instance.
AddYears Adds the specified number of years to the value of this instance.
Compare Compares two instances of DateTime and returns an indication of their relative values.
CompareTo Compares this instance to a specified object and returns an indication of their relative values.
DaysInMonth Returns the number of days in the specified month of the specified year.
Equals Overloaded:
Equals(object value)

Overridden:
Returns a value indicating whether this instance is equal to a specified object.
Equals Overloaded:
Equals(DateTime t1, DateTime t2)

Returns a value indicating whether two instances of DateTime are equal.
FromFileTime Returns a DateTime equivalent to the specified operating system file timestamp.
FromOADate Returns a DateTime equivalent to the specified OLE Automation Date.
GetDateTimeFormats Overloaded:
GetDateTimeFormats()

Converts the value of this instance to all the string representations supported by the standard DateTime format specifiers.
GetDateTimeFormats Overloaded:
GetDateTimeFormats(char format)

Converts the value of this instance to all the string representations supported by the specified standard DateTime format specifier.
GetDateTimeFormats Overloaded:
GetDateTimeFormats(IFormatProvider provider)

Converts the value of this instance to all the string representations supported by the standard DateTime format specifiers and the specified culture-specific formatting information.
GetDateTimeFormats Overloaded:
GetDateTimeFormats(char format, IFormatProvider provider)

Converts the value of this instance to all the string representations supported by the specified standard DateTime format specifier and culture-specific formatting information.
GetHashCode Overridden:
Returns the hash code for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
GetTypeCode Returns the TypeCode for value type DateTime.
IsLeapYear Returns an indication whether the specified year is a leap year.
Parse Overloaded:
Parse(string s)

Converts the specified string representation of a date and time to its DateTime equivalent.
Parse Overloaded:
Parse(string s, IFormatProvider provider)

Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information.
Parse Overloaded:
Parse(string s, IFormatProvider provider, DateTimeStyles styles)

Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style.
ParseExact Overloaded:
ParseExact(string s, string format, IFormatProvider provider)

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
ParseExact Overloaded:
ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style)

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.
ParseExact Overloaded:
ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style)

Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly.
Subtract Overloaded:
Subtract(DateTime value)

Subtracts the specified date and time from this instance.
Subtract Overloaded:
Subtract(TimeSpan value)

Subtracts the specified duration from this instance.
ToFileTime Converts the value of this instance to the format of the local system file time.
ToLocalTime Converts the current coordinated universal time (UTC) to local time.
ToLongDateString Converts the value of this instance to its equivalent long date string representation.
ToLongTimeString Converts the value of this instance to its equivalent long time string representation.
ToOADate Converts the value of this instance to the equivalent OLE Automation date.
ToShortDateString Converts the value of this instance to its equivalent short date string representation.
ToShortTimeString Converts the value of this instance to its equivalent short time string representation.
ToString Overloaded:
ToString()

Overridden:
Converts the value of this instance to its equivalent string.
ToString Overloaded:
ToString(IFormatProvider provider)

Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.
ToString Overloaded:
ToString(string format)

Converts the value of this instance to its equivalent string representation using the specified format.
ToString Overloaded:
ToString(string format, IFormatProvider provider)

Converts the value of this instance to its equivalent string representation using the specified format and culture-specific format information.
ToUniversalTime Converts the current local time to coordinated universal time (UTC).
Public Operators and Type Conversions
op_Addition Adds a specified time interval to a specified date and time, yielding a new date and time.
op_Equality Determines whether two specified instances of DateTime are equal.
op_GreaterThan Determines whether one specified DateTime is greater than another specified DateTime.
op_GreaterThanOrEqual Determines whether one specified DateTime is greater than or equal to another specified DateTime.
op_Inequality Determines whether two specified instances of DateTime are not equal.
op_LessThan Determines whether one specified DateTime is less than another specified DateTime.
op_LessThanOrEqual Determines whether one specified DateTime is less than or equal to another specified DateTime.
op_Subtraction Overloaded:
op_Subtraction

Subtracts a specified date and time from another specified date and time, yielding a time interval.
op_Subtraction Overloaded:
op_Subtraction

Subtracts a specified time interval from a specified date and time, yielding a new date and time.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.DateTime Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the DateTime structure to a specified number of ticks.
C# Syntax:
public DateTime(
   long ticks
);
Parameters:

ticks

A date and time expressed in 100-nanosecond units.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException ticks is less than DateTime.MinValue or greater than DateTime.MaxValue.
Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the DateTime structure to the specified year, month, and day.
C# Syntax:
public DateTime(
   int year,
   int month,
   int day
);
Parameters:

year

The year (1 through 9999).

month

The month (1 through 12).

day

The day (1 through the number of days in month).

Exceptions
Exception Type Condition
ArgumentOutOfRangeException year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

ArgumentException The specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue.
Remarks
The time of day for the resulting DateTime is midnight (00:00:00).
Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified Calendar.
C# Syntax:
public DateTime(
   int year,
   int month,
   int day,
   Calendar calendar
);
Parameters:

year

The year (1 through 9999).

month

The month (1 through the number of months in calendar).

day

The day (1 through the number of days in month).

calendar

The Calendar that applies to this DateTime.

Exceptions
Exception Type Condition
ArgumentNullException calendar is null
ArgumentOutOfRangeException year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

ArgumentException Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue.
Remarks
The time of day for the resulting DateTime is midnight (00:00:00).

The allowable values for year, month, and day depend on the specified Calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    
See also:
Int32 | Calendar

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.
C# Syntax:
public DateTime(
   int year,
   int month,
   int day,
   int hour,
   int minute,
   int second
);
Parameters:

year

The year (1 through 9999).

month

The month (1 through 12)

day

The day (1 through the number of days in month).

hour

The hours (0 through 23).

minute

The minutes (0 through 59).

second

The seconds (0 through 59).

Exceptions
Exception Type Condition
ArgumentOutOfRangeException year is less than 1 or greater than 9999 -or-

month is less than 1 or greater than 12

-or-

day is less than 1 or greater than the number of days in month

-or-

hour is less than 0 or greater than 23

-or-

minute is less than 0 or greater than 59

-or-

second is less than 0 or greater than 59

ArgumentException Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue.
Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    

Return to top


Overloaded ctor #5
Summary
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified Calendar.
C# Syntax:
public DateTime(
   int year,
   int month,
   int day,
   int hour,
   int minute,
   int second,
   Calendar calendar
);
Parameters:

year

The year (1 through 9999).

month

The month (1 through the number of months in calendar).

day

The day (1 through the number of days in month).

hour

The hours (0 through 23).

minute

The minutes (0 through 59).

second

The seconds (0 through 59).

calendar

The Calendar that applies to this DateTime.

Exceptions
Exception Type Condition
ArgumentNullException calendar is null.
ArgumentOutOfRangeException year is less than 1 or greater than 9999. -or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23

-or-

minute is less than 0 or greater than 59

-or-

second is less than 0 or greater than 59

ArgumentException Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue.
Remarks
The allowable values for year, month, and day depend on the specified Calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    
See also:
Int32 | Calendar

Return to top


Overloaded ctor #6
Summary
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.
C# Syntax:
public DateTime(
   int year,
   int month,
   int day,
   int hour,
   int minute,
   int second,
   int millisecond
);
Parameters:

year

The year (1 through 9999).

month

The month (1 through 12).

day

The day (1 through the number of days in month).

hour

The hours (0 through 23).

minute

The minutes (0 through 59).

second

The seconds (0 through 59).

millisecond

The milliseconds.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException year is less than 1 or greater than 9999. -or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

ArgumentException Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue.
Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    
See also:
Int32

Return to top


Overloaded ctor #7
Summary
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified Calendar.
C# Syntax:
public DateTime(
   int year,
   int month,
   int day,
   int hour,
   int minute,
   int second,
   int millisecond,
   Calendar calendar
);
Parameters:

year

The year (1 through 9999).

month

The month (1 through the number of months in calendar).

day

The day (1 through the number of days in month).

hour

The hours (0 through 23).

minute

The minutes (0 through 59).

second

The seconds (0 through 59).

millisecond

The milliseconds.

calendar

The Calendar that applies to this DateTime.

Exceptions
Exception Type Condition
ArgumentNullException calendar is null.
ArgumentOutOfRangeException year is less than 1 or greater than 9999. -or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

ArgumentException Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue.
Remarks
The allowable values for year, month, and day depend on the specified Calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

Example
The following sample demonstrates one of the DateTime.#ctor constructors.
			// Create a new instance of DateTime containing the date 
			// 7/28/1979 at 10:35:05 PM using the en-US calendar.
			System.Globalization.CultureInfo info =
				new System.Globalization.CultureInfo("en-US", false);
			
			System.Globalization.Calendar calendar = info.Calendar;
				
			System.DateTime dateTime = 
				new System.DateTime(1979,		// Year
									07,			// Month
									28,			// Day
									22,			// Hour
									35,			// Minute
									5,			// Second
									15,			// Millisecond
									calendar	// Calendar
									);  

			// Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM".
			System.Console.WriteLine("{0:F}", dateTime);

    
See also:
Int32 | Calendar

Return to top


Field: MaxValue
Summary
Represents the largest possible value of DateTime. This field is read-only.
C# Syntax:
public static readonly DateTime MaxValue;
Remarks
The value of this constant is equivalent to 23:59:59.9999999, December 31, 9999, exactly one 100-nanosecond tick before 00:00:00, January 1, 10000.

Return to top


Field: MinValue
Summary
Represents the smallest possible value of DateTime. This field is read-only.
C# Syntax:
public static readonly DateTime MinValue;
Remarks
The value of this constant is equivalent to 00:00:00.0000000, January 1, 0001.

Return to top


Property: Date (read-only)
Summary
Gets the date component of this instance.
C# Syntax:
public DateTime Date {get;}

Return to top


Property: Day (read-only)
Summary
Gets the day of the month represented by this instance.
C# Syntax:
public int Day {get;}
Example
The following sample demonstrates the DateTime.Day property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    

Return to top


Property: DayOfWeek (read-only)
Summary
Gets the day of the week represented by this instance.
C# Syntax:
public DayOfWeek DayOfWeek {get;}
See also:
DateTime.Ticks | Int32

Return to top


Property: DayOfYear (read-only)
Summary
Gets the day of the year represented by this instance.
C# Syntax:
public int DayOfYear {get;}

Return to top


Property: Hour (read-only)
Summary
Gets the hour component of the date represented by this instance.
C# Syntax:
public int Hour {get;}
Example
The following sample demonstrates the DateTime.Hour property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    

Return to top


Property: Millisecond (read-only)
Summary
Gets the milliseconds component of the date represented by this instance.
C# Syntax:
public int Millisecond {get;}
Example
The following sample demonstrates the DateTime.Millisecond property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    

Return to top


Property: Minute (read-only)
Summary
Gets the minute component of the date represented by this instance.
C# Syntax:
public int Minute {get;}
Example
The following sample demonstrates the DateTime.Minute property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    

Return to top


Property: Month (read-only)
Summary
Gets the month component of the date represented by this instance.
C# Syntax:
public int Month {get;}
Example
The following sample demonstrates the DateTime.Month property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    

Return to top


Property: Now (read-only)
Summary
Gets a DateTime that is the current local date and time on this computer.
C# Syntax:
public static DateTime Now {get;}
Remarks
The resolution of this property depends on the system timer.

System Approximate Resolution
Windows NT 3.5 and later 10 milliseconds
Windows NT 3.1 16 milliseconds
Windows 98 55 milliseconds

Return to top


Property: Second (read-only)
Summary
Gets the seconds component of the date represented by this instance.
C# Syntax:
public int Second {get;}
Example
The following sample demonstrates the DateTime.Second property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    

Return to top


Property: Ticks (read-only)
Summary
Gets the number of ticks that represent the date and time of this instance.
C# Syntax:
public long Ticks {get;}
Remarks
The value of this property is the number of 100-nanosecond intervals that have elapsed since 12:00 A.M., January 1, 0001.

Return to top


Property: TimeOfDay (read-only)
Summary
Gets the time of day for this instance.
C# Syntax:
public TimeSpan TimeOfDay {get;}
See also:
TimeSpan

Return to top


Property: Today (read-only)
Summary
Gets the current date.
C# Syntax:
public static DateTime Today {get;}

Return to top


Property: UtcNow (read-only)
Summary
Gets a DateTime that is the current local date and time on this computer expressed as the coordinated universal time (UTC).
C# Syntax:
public static DateTime UtcNow {get;}
Remarks
The resolution of this property depends on the system timer.

System Approximate Resolution
Windows NT 3.5 and later 10 milliseconds
Windows NT 3.1 16 milliseconds
Windows 98 55 milliseconds
See also:
TimeZone.GetUtcOffset

Return to top


Property: Year (read-only)
Summary
Gets the year component of the date represented by this instance.
C# Syntax:
public int Year {get;}
Example
The following sample demonstrates the DateTime.Year property.
			System.DateTime moment = new System.DateTime(
											1999, 1, 13, 3, 57, 32, 11);
			// Year gets 1999.
			int year = moment.Year;
			
			// Month gets 1 (January).
			int month = moment.Month;
			
			// Day gets 13.
			int day = moment.Day;
			
			// Hour gets 3.
			int hour = moment.Hour;
			
			// Minute gets 57.
			int minute = moment.Minute;
			
			// Second gets 32.
			int second = moment.Second;
			
			// Millisecond gets 11.
			int millisecond = moment.Millisecond;

    
See also:
Int32 | Calendar

Return to top


Method: Add(
   TimeSpan value
)
Summary
Adds the value of the specified TimeSpan to the value of this instance.
C# Syntax:
public DateTime Add(
   TimeSpan value
);
Parameters:

value

A TimeSpan that contains the interval to add.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the time interval represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.
Example
The following sample demonstrates the DateTime.Add method. It calculates the day of the week that is 36 days (864 hours) from this moment.
			// Calculate what day of the week is 36 days from this instant.
			System.DateTime today = System.DateTime.Now;
			System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0);
			System.DateTime answer = today.Add(duration);
			System.Console.WriteLine("{0:dddd}", answer);

    
See also:
TimeSpan | TimeSpan.Add

Return to top


Method: AddDays(
   double value
)
Summary
Adds the specified number of days to the value of this instance.
C# Syntax:
public DateTime AddDays(
   double value
);
Parameters:

value

A number of whole and fractional days. The value parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the number of days represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

The fractional part of value is the fractional part of a day. For example, 4.5 is equivalent to 4 days, 12 hours, 0 minutes, 0 seconds, 0 milliseconds, and 0 ticks.

The value parameter is rounded to the nearest tick.

Example
The following sample demonstrates the DateTime.AddDays method.
			// Calculate what day of the week is 36 days from this instant.
			System.DateTime today = System.DateTime.Now;
			System.DateTime answer = today.AddDays(36);
			System.Console.WriteLine("{0:dddd}", answer);

    
See also:
Double

Return to top


Method: AddHours(
   double value
)
Summary
Adds the specified number of hours to the value of this instance.
C# Syntax:
public DateTime AddHours(
   double value
);
Parameters:

value

A number of whole and fractional hours. The value parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the number of hours represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

The fractional part of value is the fractional part of an hour. For example, 4.5 is equivalent to 4 hours, 30 minutes, 0 seconds, 0 milliseconds, and 0 ticks.

The value parameter is rounded to the nearest tick.

See also:
Double

Return to top


Method: AddMilliseconds(
   double value
)
Summary
Adds the specified number of milliseconds to the value of this instance.
C# Syntax:
public DateTime AddMilliseconds(
   double value
);
Parameters:

value

A number of whole and fractional milliseconds. The value parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

The fractional part of value is the fractional part of a millisecond. For example, 4.5 is equivalent to 4 milliseconds, and 5000 ticks (where one millisecond = 10000 ticks).

The value parameter is rounded to the nearest tick.

See also:
Double

Return to top


Method: AddMinutes(
   double value
)
Summary
Adds the specified number of minutes to the value of this instance.
C# Syntax:
public DateTime AddMinutes(
   double value
);
Parameters:

value

A number of whole and fractional minutes. The value parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the number of minutes represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

The fractional part of value is the fractional part of a minute. For example, 4.5 is equivalent to 4 minutes, 30 seconds, 0 milliseconds, and 0 ticks.

The value parameter is rounded to the nearest tick.

See also:
Double

Return to top


Method: AddMonths(
   int months
)
Summary
Adds the specified number of months to the value of this instance.
C# Syntax:
public DateTime AddMonths(
   int months
);
Parameters:

months

A number of months. The months parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and months.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.

-or-

months is less than -120,000 or greater than 120,000.

Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.
See also:
Double

Return to top


Method: AddSeconds(
   double value
)
Summary
Adds the specified number of seconds to the value of this instance.
C# Syntax:
public DateTime AddSeconds(
   double value
);
Parameters:

value

A number of whole and fractional seconds. The value parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

The fractional part of value is the fractional part of a second. For example, 4.5 is equivalent to 4 seconds, 500 milliseconds, and 0 ticks.

The value parameter is rounded to the nearest tick.

See also:
Double

Return to top


Method: AddTicks(
   long value
)
Summary
Adds the specified number of ticks to the value of this instance.
C# Syntax:
public DateTime AddTicks(
   long value
);
Parameters:

value

A number of 100-nanosecond ticks. The value parameter can be positive or negative.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the time represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.
See also:
Int64 | DateTime.Ticks

Return to top


Method: AddYears(
   int value
)
Summary
Adds the specified number of years to the value of this instance.
C# Syntax:
public DateTime AddYears(
   int value
);
Parameters:

value

A number of years. The value parameter can be negative or positive.

Return Value:
A DateTime whose value is the sum of the date and time represented by this instance and the number of years represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException value or the resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.
See also:
Double

Return to top


Method: Compare(
   DateTime t1,
   DateTime t2
)
Summary
Compares two instances of DateTime and returns an indication of their relative values.
C# Syntax:
public static int Compare(
   DateTime t1,
   DateTime t2
);
Parameters:

t1

The first DateTime.

t2

The second DateTime.

Return Value:
A signed number indicating the relative values of t1 and t2.

Value Type Condition
Less than zero is less than .
Zero equals .
Greater than zero is greater than .
Example
The following sample demonstrates DateTime.Compare.
        DateTime t1 = new DateTime(100);
        DateTime t2 = new DateTime(20);

        if (DateTime.Compare(t1, t2) >  0) Console.WriteLine("t1 > t2"); 
        if (DateTime.Compare(t1, t2) == 0) Console.WriteLine("t1 == t2"); 
        if (DateTime.Compare(t1, t2) <  0) Console.WriteLine("t1 < t2");

    
See also:
DateTime.CompareTo | DateTime.Equals

Return to top


Method: CompareTo(
   object value
)
Summary
Compares this instance to a specified object and returns an indication of their relative values.
C# Syntax:
public int CompareTo(
   object value
);
Parameters:

value

An object to compare, or null.

Return Value:
A signed number indicating the relative values of this instance and value.

Value Description
Less than zero This instance is less than .
Zero This instance is equal to .
Greater than zero This instance is greater than , or is null .
Exceptions
Exception Type Condition
ArgumentException value is not a DateTime.
Remarks
Any instance of DateTime, regardless of its value, is considered greater than null.

value must be an instance of DateTime or null; otherwise, an exception is thrown.

Example
The following sample demonstrates the DateTime.CompareTo method.
			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);
			}

    
See also:
DateTime.Compare | DateTime.Equals

Return to top


Method: DaysInMonth(
   int year,
   int month
)
Summary
Returns the number of days in the specified month of the specified year.
C# Syntax:
public static int DaysInMonth(
   int year,
   int month
);
Parameters:

year

The year.

month

The month (a number ranging from 1 to 12).

Return Value:
The number of days in month for the specified year.

For example, if month equals 2 for February, the return value is 28 or 29 depending upon whether year is a leap year.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException month is less than 1 or greater than 12.
Example
The following sample demonstrates the DateTime.DaysInMonth method.
			const int July = 7;
			const int Feb = 2;

			// daysInJuly gets 31.
			int daysInJuly = System.DateTime.DaysInMonth(2001, July);

			// daysInFeb gets 28 because the year 1998 was not a leap year.
			int daysInFeb = System.DateTime.DaysInMonth(1998, Feb);

			// daysInFebLeap gets 29 because the year 1996 was a leap year.
			int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb);

    

Return to top


Overloaded Method: Equals(
   object value
)
Summary
Returns a value indicating whether this instance is equal to a specified object.
C# Syntax:
public override bool Equals(
   object value
);
Parameters:

value

An object to compare with this instance.

Return Value:
true if value is an instance of DateTime and equals the value of this instance; otherwise, false.
Example
The following sample demonstrates the DateTime.Equals method.
			System.DateTime today1 = 
					new System.DateTime(System.DateTime.Today.Ticks);
			System.DateTime today2 = 
					new System.DateTime(System.DateTime.Today.Ticks);
			System.DateTime tomorrow = 
					new System.DateTime(
								System.DateTime.Today.AddDays(1).Ticks);

			// todayEqualsToday gets true.
			bool todayEqualsToday = System.DateTime.Equals(today1, today2);

			// todayEqualsTomorrow gets false.
			bool todayEqualsTomorrow = System.DateTime.Equals(today1, tomorrow);

    
See also:
DateTime.Compare | DateTime.CompareTo

Return to top


Overloaded Method: Equals(
   DateTime t1,
   DateTime t2
)
Summary
Returns a value indicating whether two instances of DateTime are equal.
C# Syntax:
public static bool Equals(
   DateTime t1,
   DateTime t2
);
Parameters:

t1

The first DateTime.

t2

The second DateTime.

Return Value:
true if the two DateTime values are equal; otherwise, false.
Example
The following sample demonstrates the DateTime.Equals method.
			System.DateTime today1 = 
					new System.DateTime(System.DateTime.Today.Ticks);
			System.DateTime today2 = 
					new System.DateTime(System.DateTime.Today.Ticks);
			System.DateTime tomorrow = 
					new System.DateTime(
								System.DateTime.Today.AddDays(1).Ticks);

			// todayEqualsToday gets true.
			bool todayEqualsToday = System.DateTime.Equals(today1, today2);

			// todayEqualsTomorrow gets false.
			bool todayEqualsTomorrow = System.DateTime.Equals(today1, tomorrow);

    
See also:
Boolean | DateTime.Compare | DateTime.CompareTo

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~DateTime();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: FromFileTime(
   long fileTime
)
Summary
Returns a DateTime equivalent to the specified operating system file timestamp.
C# Syntax:
public static DateTime FromFileTime(
   long fileTime
);
Parameters:

fileTime

A Windows file time.

Return Value:
A DateTime value representing the date and time of fileTime, adjusted to local time.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException fileTime is not valid.
Remarks
fileTime is a 64-bit signed integer value representing a Windows file timestamp. The timestamp is the number of 100-nanosecond intervals that have elapsed since January 1, 1601 12:00 A.M. coordinated universal time (UTC).
Example
The following sample demonstrates the DateTime.FromFileTime method.
		public System.TimeSpan FileAge(long fileCreationTime) {

			System.DateTime now = System.DateTime.Now;
			try {
				System.DateTime fCreationTime = 
					System.DateTime.FromFileTime(fileCreationTime);
				System.TimeSpan fileAge = now.Subtract(fCreationTime);
				return fileAge;				
			} 
			catch (ArgumentOutOfRangeException) {
				// fileCreationTime is not valid, so re-throw the exception.
				throw;
			}
		}

    
See also:
DateTime.ToUniversalTime | TimeZone.GetUtcOffset

Return to top


Method: FromOADate(
   double d
)
Summary
Returns a DateTime equivalent to the specified OLE Automation Date.
C# Syntax:
public static DateTime FromOADate(
   double d
);
Parameters:

d

An OLE Automation Date value.

Return Value:
A DateTime that represents the same date and time as d.
Exceptions
Exception Type Condition
ArgumentException The date is not a valid OLE Automation Date value.
Remarks
d must be a value between negative 657435.0 through positive 2958466.0. It is stored as a double-precision floating-point number.

See DateTime.ToOADate and the MSDN Online Library at http://MSDN.Microsoft.com/library/default.asp for more information on OLE Automation.

See also:
DateTime.ToOADate

Return to top


Overloaded Method: GetDateTimeFormats()
Summary
Converts the value of this instance to all the string representations supported by the standard DateTime format specifiers.
C# Syntax:
public string[] GetDateTimeFormats();
Return Value:
A string array where each element is the representation of the value of this instance formatted with one of the standard DateTime formatting specifiers.
Remarks
Each element of the return value is formatted using information from the current culture. For more information about culture-specific formatting information for the current culture, see CultureInfo.CurrentCulture.

For more information about the standard formatting specifiers, see DateTimeFormatInfo.

Example
The following sample demonstrates the DateTime.GetDateTimeFormats method.
			DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16);

			string[] july28Formats = july28.GetDateTimeFormats();

			// Print out july28 in all DateTime formats using the
			// default culture.
			foreach (string format in july28Formats) {
				System.Console.WriteLine(format);
			}

			IFormatProvider culture = 
				new System.Globalization.CultureInfo("fr-FR", true);
			// Get the short date formats using the "fr-FR" culture.
			string [] frenchJuly28Formats = 
						july28.GetDateTimeFormats('d', culture);

			// Print out july28 in various formats using "fr-FR" culture.
			System.Console.WriteLine("Starting fr-FR formats");
			foreach (string format in frenchJuly28Formats) {
				System.Console.WriteLine(format);
			}

    

Return to top


Overloaded Method: GetDateTimeFormats(
   char format
)
Summary
Converts the value of this instance to all the string representations supported by the specified standard DateTime format specifier.
C# Syntax:
public string[] GetDateTimeFormats(
   char format
);
Parameters:

format

A Unicode character containing a format specifier.

Return Value:
A string array where each element is the representation of the value of this instance formatted with the format standard DateTime formatting specifier.
Remarks
Each element of the return value is formatted using information from the current culture. For more information about culture-specific formatting information for the current culture, see CultureInfo.CurrentCulture.

For more information about the standard formatting specifiers, see DateTimeFormatInfo.

Example
The following sample demonstrates the DateTime.GetDateTimeFormats method.
			DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16);

			string[] july28Formats = july28.GetDateTimeFormats();

			// Print out july28 in all DateTime formats using the
			// default culture.
			foreach (string format in july28Formats) {
				System.Console.WriteLine(format);
			}

			IFormatProvider culture = 
				new System.Globalization.CultureInfo("fr-FR", true);
			// Get the short date formats using the "fr-FR" culture.
			string [] frenchJuly28Formats = 
						july28.GetDateTimeFormats('d', culture);

			// Print out july28 in various formats using "fr-FR" culture.
			System.Console.WriteLine("Starting fr-FR formats");
			foreach (string format in frenchJuly28Formats) {
				System.Console.WriteLine(format);
			}

    

Return to top


Overloaded Method: GetDateTimeFormats(
   IFormatProvider provider
)
Summary
Converts the value of this instance to all the string representations supported by the standard DateTime format specifiers and the specified culture-specific formatting information.
C# Syntax:
public string[] GetDateTimeFormats(
   IFormatProvider provider
);
Parameters:

provider

An IFormatProvider that supplies culture-specific formatting information about this instance.

Return Value:
A string array where each element is the representation of the value of this instance formatted with one of the standard DateTime formatting specifiers.
Remarks
Each element of the return value is formatted using culture-specific information supplied by provider.

For more information about the standard formatting specifiers, see DateTimeFormatInfo.

Example
The following sample demonstrates the DateTime.GetDateTimeFormats method.
			DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16);

			string[] july28Formats = july28.GetDateTimeFormats();

			// Print out july28 in all DateTime formats using the
			// default culture.
			foreach (string format in july28Formats) {
				System.Console.WriteLine(format);
			}

			IFormatProvider culture = 
				new System.Globalization.CultureInfo("fr-FR", true);
			// Get the short date formats using the "fr-FR" culture.
			string [] frenchJuly28Formats = 
						july28.GetDateTimeFormats('d', culture);

			// Print out july28 in various formats using "fr-FR" culture.
			System.Console.WriteLine("Starting fr-FR formats");
			foreach (string format in frenchJuly28Formats) {
				System.Console.WriteLine(format);
			}

    

Return to top


Overloaded Method: GetDateTimeFormats(
   char format,
   IFormatProvider provider
)
Summary
Converts the value of this instance to all the string representations supported by the specified standard DateTime format specifier and culture-specific formatting information.
C# Syntax:
public string[] GetDateTimeFormats(
   char format,
   IFormatProvider provider
);
Parameters:

format

A Unicode character containing a format specifier.

provider

An IFormatProvider that supplies culture-specific formatting information about this instance.

Return Value:
A string array where each element is the representation of the value of this instance formatted with one of the standard DateTime formatting specifiers.
Remarks
Each element of the return value is formatted using culture-specific information supplied by provider.

For more information about the standard formatting specifiers, see DateTimeFormatInfo.

Example
The following sample demonstrates the DateTime.GetDateTimeFormats method.
			DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16);

			string[] july28Formats = july28.GetDateTimeFormats();

			// Print out july28 in all DateTime formats using the
			// default culture.
			foreach (string format in july28Formats) {
				System.Console.WriteLine(format);
			}

			IFormatProvider culture = 
				new System.Globalization.CultureInfo("fr-FR", true);
			// Get the short date formats using the "fr-FR" culture.
			string [] frenchJuly28Formats = 
						july28.GetDateTimeFormats('d', culture);

			// Print out july28 in various formats using "fr-FR" culture.
			System.Console.WriteLine("Starting fr-FR formats");
			foreach (string format in frenchJuly28Formats) {
				System.Console.WriteLine(format);
			}

    

Return to top


Overridden Method: GetHashCode()
Summary
Returns the hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
A 32-bit signed integer hash code.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetTypeCode()
Summary
Returns the TypeCode for value type DateTime.
C# Syntax:
public TypeCode GetTypeCode();
Return Value:
The enumerated constant, TypeCode.DateTime.

Return to top


Method: IsLeapYear(
   int year
)
Summary
Returns an indication whether the specified year is a leap year.
C# Syntax:
public static bool IsLeapYear(
   int year
);
Parameters:

year

A 4-digit year.

Return Value:
true if year is a leap year; otherwise, false.
Remarks
year is specified as a 4-digit base 10 number; for example, 1996.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: op_Addition(
   DateTime d,
   TimeSpan t
)
Summary
Adds a specified time interval to a specified date and time, yielding a new date and time.
C# Syntax:
public static DateTime operator +(
   DateTime d,
   TimeSpan t
);
Parameters:

d

A DateTime.

t

A TimeSpan.

Return Value:
A DateTime that is the sum of the values of d and t.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.
Example
The following sample demonstrates the addition operator.
			System.DateTime dTime = new System.DateTime(1980, 8, 5);

			// tSpan is 17 days, 4 hours, 2 minutes and 1 second.
			System.TimeSpan tSpan 
				= new System.TimeSpan(17, 4, 2, 1); 

			// Result gets 8/22/1980 4:02:01 AM.
			System.DateTime result = dTime + tSpan;

    
See also:
TimeSpan | DateTime.Ticks

Return to top


Method: op_Equality(
   DateTime d1,
   DateTime d2
)
Summary
Determines whether two specified instances of DateTime are equal.
C# Syntax:
public static bool operator ==(
   DateTime d1,
   DateTime d2
);
Parameters:

d1

A DateTime.

d2

A DateTime.

Return Value:
true if d1 and d2 represent the same date and time; otherwise, false.
Example
The following sample demonstrates the equality operator.
			System.DateTime april19 = new DateTime(2001, 4, 19);
			System.DateTime otherDate = new DateTime(1991, 6, 5);

			// areEqual gets false.
			bool areEqual = april19 == otherDate;
				
			otherDate = new DateTime(2001, 4, 19);
			// areEqual gets true.
			areEqual = april19 == otherDate;

    
See also:
Boolean

Return to top


Method: op_GreaterThan(
   DateTime t1,
   DateTime t2
)
Summary
Determines whether one specified DateTime is greater than another specified DateTime.
C# Syntax:
public static bool operator >(
   DateTime t1,
   DateTime t2
);
Parameters:

t1

A DateTime. A DateTime.

t2

A DateTime. A DateTime.

Return Value:
true if t1 is greater than t2; otherwise, false.
See also:
Boolean

Return to top


Method: op_GreaterThanOrEqual(
   DateTime t1,
   DateTime t2
)
Summary
Determines whether one specified DateTime is greater than or equal to another specified DateTime.
C# Syntax:
public static bool operator >=(
   DateTime t1,
   DateTime t2
);
Parameters:

t1

A DateTime. A DateTime.

t2

A DateTime.

Return Value:
true if t1 is greater than or equal to t2; otherwise, false.
See also:
Boolean

Return to top


Method: op_Inequality(
   DateTime d1,
   DateTime d2
)
Summary
Determines whether two specified instances of DateTime are not equal.
C# Syntax:
public static bool operator !=(
   DateTime d1,
   DateTime d2
);
Parameters:

d1

A DateTime.

d2

A DateTime.

Return Value:
true if d1 and d2 do not represent the same date and time; otherwise, false.
See also:
Boolean

Return to top


Method: op_LessThan(
   DateTime t1,
   DateTime t2
)
Summary
Determines whether one specified DateTime is less than another specified DateTime.
C# Syntax:
public static bool operator <(
   DateTime t1,
   DateTime t2
);
Parameters:

t1

A DateTime.

t2

A DateTime.

Return Value:
true if t1 is less than t2; otherwise, false.
See also:
Boolean

Return to top


Method: op_LessThanOrEqual(
   DateTime t1,
   DateTime t2
)
Summary
Determines whether one specified DateTime is less than or equal to another specified DateTime.
C# Syntax:
public static bool operator <=(
   DateTime t1,
   DateTime t2
);
Parameters:

t1

A DateTime.

t2

A DateTime.

Return Value:
true if t1 is less than or equal to t2; otherwise, false.
See also:
Boolean

Return to top


Overloaded Method: op_Subtraction(
   DateTime d1,
   DateTime d2
)
Summary
Subtracts a specified date and time from another specified date and time, yielding a time interval.
C# Syntax:
No member name (
   DateTime d1,
   DateTime d2
);
Parameters:

d1

A DateTime (the minuend).

d2

A DateTime (the subtrahend).

Return Value:
A TimeSpan that is the time interval between d1 and d2; that is, d1 minus d2.
Example
The following sample demonstrates the DateTime.Subtract method and operator.
			System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
			System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
			System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

			// diff1 gets 185 days, 14 hours, and 47 minutes.
			System.TimeSpan diff1 = date2.Subtract(date1);

			// date4 gets 4/9/1996 5:55:00 PM.
			System.DateTime date4 = date3.Subtract(diff1);

			// diff2 gets 55 days 4 hours and 20 minutes.
			System.TimeSpan diff2 = date2 - date3;

			// date5 gets 4/9/1996 5:55:00 PM.
			System.DateTime date5 = date1 - diff2;

    
See also:
DateTime.Ticks

Return to top


Overloaded Method: op_Subtraction(
   DateTime d,
   TimeSpan t
)
Summary
Subtracts a specified time interval from a specified date and time, yielding a new date and time.
C# Syntax:
No member name (
   DateTime d,
   TimeSpan t
);
Parameters:

d

A DateTime.

t

A TimeSpan.

Return Value:
A DateTime whose value is the value of d minus the value of t.
Remarks
This method subtracts the ticks values of t from the ticks value of d.
Example
The following sample demonstrates the DateTime.Subtract method and operator.
			System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
			System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
			System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

			// diff1 gets 185 days, 14 hours, and 47 minutes.
			System.TimeSpan diff1 = date2.Subtract(date1);

			// date4 gets 4/9/1996 5:55:00 PM.
			System.DateTime date4 = date3.Subtract(diff1);

			// diff2 gets 55 days 4 hours and 20 minutes.
			System.TimeSpan diff2 = date2 - date3;

			// date5 gets 4/9/1996 5:55:00 PM.
			System.DateTime date5 = date1 - diff2;

    
See also:
DateTime.Ticks | TimeSpan

Return to top


Overloaded Method: Parse(
   string s
)
Summary
Converts the specified string representation of a date and time to its DateTime equivalent.
C# Syntax:
public static DateTime Parse(
   string s
);
Parameters:

s

A string containinga date and time to convert.

Return Value:
A DateTime equivalent to the date and time contained in s.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s does not contain a valid string representation of a date and time.
Remarks
The string s is parsed using the formatting information in a DateTimeFormatInfo initialized for the current culture.

This method attempts to parse s completely and avoid throwing FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes 12 A.M. Any leading, inner, or trailing white space character in s is ignored.

Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

Example
The following sample demonstrates the DateTime.Parse method.
			string strMyDateTime = "2/16/1992 12:15:12";

			// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
			System.DateTime myDateTime =
				System.DateTime.Parse(strMyDateTime);

			System.IFormatProvider format =
				new System.Globalization.CultureInfo("fr-FR", true);

			// Reverse month and day to conform to a different format.
			string strMyDateTimeFrench = "    16/02/1992 12:15:12";

			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			System.DateTime myDateTimeFrench =
				System.DateTime.Parse(strMyDateTimeFrench,
									  format,
									  System.Globalization.
										DateTimeStyles.NoCurrentDateDefault);
	
			string[] expectedFormats = {"G", "g", "f" ,"F"};
			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			myDateTimeFrench = 
				System.DateTime.ParseExact(strMyDateTimeFrench,
										   expectedFormats,
										   format,
										   System.Globalization.
											DateTimeStyles.AllowWhiteSpaces);

    
See also:
MSDN: formattingoverview | String | DateTime.ParseExact | CultureInfo | DateTimeFormatInfo.CurrentInfo

Return to top


Overloaded Method: Parse(
   string s,
   IFormatProvider provider
)
Summary
Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information.
C# Syntax:
public static DateTime Parse(
   string s,
   IFormatProvider provider
);
Parameters:

s

A string containinga date and time to convert.

provider

An IFormatProvider that supplies culture-specific format information about s.

Return Value:
A DateTime equivalent to the date and time contained in s as specified by provider.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s does not contain a valid string representation of a date and time.
Remarks
The string s is parsed using the formatting information in a DateTimeFormatInfo supplied by provider.

This method attempts to parse s completely and avoid throwing FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes 12 A.M. Any leading, inner, or trailing white space character in s is ignored.

Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.

Example
The following sample demonstrates the DateTime.Parse method.
			string strMyDateTime = "2/16/1992 12:15:12";

			// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
			System.DateTime myDateTime =
				System.DateTime.Parse(strMyDateTime);

			System.IFormatProvider format =
				new System.Globalization.CultureInfo("fr-FR", true);

			// Reverse month and day to conform to a different format.
			string strMyDateTimeFrench = "    16/02/1992 12:15:12";

			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			System.DateTime myDateTimeFrench =
				System.DateTime.Parse(strMyDateTimeFrench,
									  format,
									  System.Globalization.
										DateTimeStyles.NoCurrentDateDefault);
	
			string[] expectedFormats = {"G", "g", "f" ,"F"};
			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			myDateTimeFrench = 
				System.DateTime.ParseExact(strMyDateTimeFrench,
										   expectedFormats,
										   format,
										   System.Globalization.
											DateTimeStyles.AllowWhiteSpaces);

    
See also:
MSDN: formattingoverview | String | DateTime.ParseExact | CultureInfo | DateTimeFormatInfo.CurrentInfo

Return to top


Overloaded Method: Parse(
   string s,
   IFormatProvider provider,
   DateTimeStyles styles
)
Summary
Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style.
C# Syntax:
public static DateTime Parse(
   string s,
   IFormatProvider provider,
   DateTimeStyles styles
);
Parameters:

s

A string containinga date and time to convert.

provider

An IFormatProvider that supplies culture-specific formatting information about s.

styles

The combination of one or more DateTimeStyles constants that indicate the permitted format of s.

Return Value:
A DateTime equivalent to the date and time contained in s as specified by provider and styles.
Exceptions
Exception Type Condition
ArgumentNullException s is null.
FormatException s does not contain a valid string representation of a date and time.
Remarks
The string s is parsed using the formatting information in a DateTimeFormatInfo supplied by provider.

This method attempts to ignore unrecognized data and parse s completely. If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. Any leading, inner, or trailing white space character in s is ignored.

Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.

Example
The following sample demonstrates the DateTime.Parse method.
			string strMyDateTime = "2/16/1992 12:15:12";

			// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
			System.DateTime myDateTime =
				System.DateTime.Parse(strMyDateTime);

			System.IFormatProvider format =
				new System.Globalization.CultureInfo("fr-FR", true);

			// Reverse month and day to conform to a different format.
			string strMyDateTimeFrench = "    16/02/1992 12:15:12";

			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			System.DateTime myDateTimeFrench =
				System.DateTime.Parse(strMyDateTimeFrench,
									  format,
									  System.Globalization.
										DateTimeStyles.NoCurrentDateDefault);
	
			string[] expectedFormats = {"G", "g", "f" ,"F"};
			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			myDateTimeFrench = 
				System.DateTime.ParseExact(strMyDateTimeFrench,
										   expectedFormats,
										   format,
										   System.Globalization.
											DateTimeStyles.AllowWhiteSpaces);

    
See also:
MSDN: formattingoverview | String | DateTime.ParseExact | CultureInfo | DateTimeFormatInfo.CurrentInfo

Return to top


Overloaded Method: ParseExact(
   string s,
   string format,
   IFormatProvider provider
)
Summary
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
C# Syntax:
public static DateTime ParseExact(
   string s,
   string format,
   IFormatProvider provider
);
Parameters:

s

A string containing a date and time to convert.

format

The expected format of s.

provider

An IFormatProvider that supplies culture-specific format information about s.

Return Value:
A DateTime equivalent to the date and time contained in s as specified by format and provider.
Exceptions
Exception Type Condition
ArgumentNullException s or format is null.
FormatException s or format is an empty string

-or-

s does not contain a date and time that corresponds to the pattern specified in format.

Remarks
This method throws FormatException if the format of s is not exactly as specified by the format pattern in format. If format consists of a single standard format character, the format pattern that character represents is used. For more information, see the DateTimeFormatInfo topic.

If s contains only a time, and no date, then the current date is used. If s contains only a date and no time, midnight (00:00:00) is used.

Parameter s cannot contain leading, inner, or trailing white space characters.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.

Example
The following sample demonstrates the DateTime.ParseExact method.
			string strMyDateTime = "2/16/1992 12:15:12";

			// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
			System.DateTime myDateTime =
				System.DateTime.Parse(strMyDateTime);

			System.IFormatProvider format =
				new System.Globalization.CultureInfo("fr-FR", true);

			// Reverse month and day to conform to a different format.
			string strMyDateTimeFrench = "    16/02/1992 12:15:12";

			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			System.DateTime myDateTimeFrench =
				System.DateTime.Parse(strMyDateTimeFrench,
									  format,
									  System.Globalization.
										DateTimeStyles.NoCurrentDateDefault);
	
			string[] expectedFormats = {"G", "g", "f" ,"F"};
			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			myDateTimeFrench = 
				System.DateTime.ParseExact(strMyDateTimeFrench,
										   expectedFormats,
										   format,
										   System.Globalization.
											DateTimeStyles.AllowWhiteSpaces);

    
See also:
MSDN: formattingoverview | DateTime.Parse | String | CultureInfo | DateTimeFormatInfo.CurrentInfo

Return to top


Overloaded Method: ParseExact(
   string s,
   string format,
   IFormatProvider provider,
   DateTimeStyles style
)
Summary
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.
C# Syntax:
public static DateTime ParseExact(
   string s,
   string format,
   IFormatProvider provider,
   DateTimeStyles style
);
Parameters:

s

A string containing a date and time to convert.

format

The expected format of s .

provider

An IFormatProvider that supplies culture-specific formatting information about s.

style

The combination of one or more DateTimeStyles constants that indicate the permitted format of s. The combination of one or more DateTimeStyles constants that indicate the permitted format of s.

Return Value:
A DateTime equivalent to the date and time contained in s as specified by format, provider, and style.
Exceptions
Exception Type Condition
ArgumentNullException s or format is null.
FormatException s or format is an empty string

-or-

s does not contain a date and time that corresponds to the pattern specified in format.

Remarks
This method throws FormatException if the format of s is not exactly as specified by the format pattern in format. If format consists of a single standard format character, the format pattern that character represents is used. For more information, see the DateTimeFormatInfo topic.

If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. The style parameter also determines whether leading, inner, or trailing white space characters are ignored.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.

Example
The following sample demonstrates the DateTime.ParseExact method.
			string strMyDateTime = "2/16/1992 12:15:12";

			// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
			System.DateTime myDateTime =
				System.DateTime.Parse(strMyDateTime);

			System.IFormatProvider format =
				new System.Globalization.CultureInfo("fr-FR", true);

			// Reverse month and day to conform to a different format.
			string strMyDateTimeFrench = "    16/02/1992 12:15:12";

			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			System.DateTime myDateTimeFrench =
				System.DateTime.Parse(strMyDateTimeFrench,
									  format,
									  System.Globalization.
										DateTimeStyles.NoCurrentDateDefault);
	
			string[] expectedFormats = {"G", "g", "f" ,"F"};
			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			myDateTimeFrench = 
				System.DateTime.ParseExact(strMyDateTimeFrench,
										   expectedFormats,
										   format,
										   System.Globalization.
											DateTimeStyles.AllowWhiteSpaces);

    
See also:
MSDN: formattingoverview | DateTime.Parse | String | CultureInfo | DateTimeFormatInfo.CurrentInfo

Return to top


Overloaded Method: ParseExact(
   string s,
   string[] formats,
   IFormatProvider provider,
   DateTimeStyles style
)
Summary
Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly.
C# Syntax:
public static DateTime ParseExact(
   string s,
   string[] formats,
   IFormatProvider provider,
   DateTimeStyles style
);
Parameters:

s

A string containing one or more dates and times to convert.

formats

An array of expected formats of s .

provider

An IFormatProvider that supplies culture-specific format information about s.

style

The combination of one or more DateTimeStyles constants that indicate the permitted format of s. The combination of one or more DateTimeStyles constants that indicate the permitted format of s.

Return Value:
A DateTime equivalent to the date and time contained in s as specified by formats, provider, and style.
Exceptions
Exception Type Condition
ArgumentNullException s or formats is null.
FormatException s is an empty string

-or-

an element of formats is an empty string

-or-

s does not contain a date and time that corresponds to any element of formats.

Remarks
This method throws FormatException if the format of s is not exactly as specified by at least one of the format patterns in formats. If an element of formats consists of a single standard format character, the format pattern that character represents is used. For more information, see the DateTimeFormatInfo topic.

If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. The style parameter also determines whether leading, inner, or trailing white space characters are ignored.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.

Example
The following sample demonstrates the DateTime.ParseExact method.
			string strMyDateTime = "2/16/1992 12:15:12";

			// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
			System.DateTime myDateTime =
				System.DateTime.Parse(strMyDateTime);

			System.IFormatProvider format =
				new System.Globalization.CultureInfo("fr-FR", true);

			// Reverse month and day to conform to a different format.
			string strMyDateTimeFrench = "    16/02/1992 12:15:12";

			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			System.DateTime myDateTimeFrench =
				System.DateTime.Parse(strMyDateTimeFrench,
									  format,
									  System.Globalization.
										DateTimeStyles.NoCurrentDateDefault);
	
			string[] expectedFormats = {"G", "g", "f" ,"F"};
			// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
			// 15 min and 12 sec.
			myDateTimeFrench = 
				System.DateTime.ParseExact(strMyDateTimeFrench,
										   expectedFormats,
										   format,
										   System.Globalization.
											DateTimeStyles.AllowWhiteSpaces);

    
See also:
MSDN: formattingoverview | DateTime.Parse | String | CultureInfo | DateTimeFormatInfo.CurrentInfo

Return to top


Overloaded Method: Subtract(
   DateTime value
)
Summary
Subtracts the specified date and time from this instance.
C# Syntax:
public TimeSpan Subtract(
   DateTime value
);
Parameters:

value

A instance of DateTime.

Return Value:
A TimeSpan interval equal to the date and time represented by this instance minus the date and time represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The result is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new TimeSpan is returned whose value is the result of this operation.
Example
The following sample demonstrates the DateTime.Subtract method and operator.
			System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
			System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
			System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

			// diff1 gets 185 days, 14 hours, and 47 minutes.
			System.TimeSpan diff1 = date2.Subtract(date1);

			// date4 gets 4/9/1996 5:55:00 PM.
			System.DateTime date4 = date3.Subtract(diff1);

			// diff2 gets 55 days 4 hours and 20 minutes.
			System.TimeSpan diff2 = date2 - date3;

			// date5 gets 4/9/1996 5:55:00 PM.
			System.DateTime date5 = date1 - diff2;

    

Return to top


Overloaded Method: Subtract(
   TimeSpan value
)
Summary
Subtracts the specified duration from this instance.
C# Syntax:
public DateTime Subtract(
   TimeSpan value
);
Parameters:

value

An instance of TimeSpan.

Return Value:
A DateTime equal to the date and time represented by this instance minus the time interval represented by value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The result is less than DateTime.MinValue or greater than DateTime.MaxValue.
Remarks
This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.
Example
The following sample demonstrates the DateTime.Subtract method and operator.
			System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
			System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
			System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

			// diff1 gets 185 days, 14 hours, and 47 minutes.
			System.TimeSpan diff1 = date2.Subtract(date1);

			// date4 gets 4/9/1996 5:55:00 PM.
			System.DateTime date4 = date3.Subtract(diff1);

			// diff2 gets 55 days 4 hours and 20 minutes.
			System.TimeSpan diff2 = date2 - date3;

			// date5 gets 4/9/1996 5:55:00 PM.
			System.DateTime date5 = date1 - diff2;

    
See also:
TimeSpan

Return to top


Method: ToFileTime()
Summary
Converts the value of this instance to the format of the local system file time.
C# Syntax:
public long ToFileTime();
Return Value:
The value of this DateTime in the format of the local system file time.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The value of this instance cannot be converted to a system file time.
Remarks
A system file time is a 64-bit unsigned value representing the date and time as the number of 100-nanosecond intervals that have elapsed since January 1, 1601 12:00 A.M.

An exception is thrown if you attempt to convert a date earlier than January 1, 1601 12:00:00 A.M. coordinated universal time (UTC).

Example
The following sample demonstrates DateTime.ToFileTime.
		static void Main(string[] args)
		{
			System.Console.WriteLine("Enter the file path:");
			string filePath = System.Console.ReadLine();

			if (System.IO.File.Exists(filePath)) {
				System.DateTime fileCreationDateTime = 
					System.IO.File.GetCreationTime(filePath);

				long fileCreationFileTime = fileCreationDateTime.ToFileTime();

				System.Console.WriteLine("{0} in file time is {1}.",
										 fileCreationDateTime,
										 fileCreationFileTime);
			} 
			else {
				System.Console.WriteLine("{0} is an invalid file", filePath);
			}
		}

    
See also:
Int64 | DateTime.ToUniversalTime | TimeZone.GetUtcOffset

Return to top


Method: ToLocalTime()
Summary
Converts the current coordinated universal time (UTC) to local time.
C# Syntax:
public DateTime ToLocalTime();
Return Value:
The DateTime equivalent to the current UTC time, adjusted to the local time zone and daylight saving time.

-or-

DateTime.MaxValue if the current UTC time is too large to be represented as a DateTime.

-or-

DateTime.MinValue if the current UTC time is too small to be represented as a DateTime.

Remarks
This method assumes that the current DateTime holds the UTC time value, and not a local time. Therefore, each time it is run, the current method performs the necessary modifications on the DateTime to derive the local time, whether the current DateTime holds the UTC time or not.

This method always uses the local time zone when making calculations.

Example
The following sample demonstrates DateTime.ToLocalTime.
			System.Console.WriteLine("Enter a date and time.");
			string strDateTime = System.Console.ReadLine();

			System.DateTime localDateTime;
			try {
				localDateTime = System.DateTime.Parse(strDateTime);
			}
			catch (System.FormatException) {
				System.Console.WriteLine("Invalid format.");
				return;
			}

			System.DateTime univDateTime = localDateTime.ToUniversalTime();

			System.Console.WriteLine("{0} local time is {1} universal time.",
									 localDateTime,
									 univDateTime); 
			
			System.Console.WriteLine("Enter a date and time in universal time.");
			strDateTime = System.Console.ReadLine();

			try {
				univDateTime = System.DateTime.Parse(strDateTime);
			}
			catch (System.FormatException) {
				System.Console.WriteLine("Invalid format.");
				return;
			}

			localDateTime = univDateTime.ToLocalTime();

			System.Console.WriteLine("{0} universal time is {1} local time.",
									 univDateTime,
									 localDateTime); 

    
See also:
TimeZone | TimeZone.GetUtcOffset | TimeZone.GetDaylightChanges | DateTime.ToUniversalTime

Return to top


Method: ToLongDateString()
Summary
Converts the value of this instance to its equivalent long date string representation.
C# Syntax:
public string ToLongDateString();
Return Value:
A string containing the name of the day of the week, the name of the month, the numeric day of the month, and the year equivalent to the date value of this instance.
Remarks
The value of this instance is formatted using the long date format character, 'D'. The return value is identical to the value returned by DateTime.ToString ("D", null).

For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.

This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.

Example
The following sample demonstrates DateTime.ToLongDateString.
			System.DateTime dTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

			// If the current culture is "us-EN",
			// longDateString gets "Wednesday, May 16, 2001"
			string longDateString = dTime.ToLongDateString();

			// If the current culture is "us-EN",
			// longTimeString gets "3:02:15 AM"
			string longTimeString = dTime.ToLongTimeString();			

    
See also:
MSDN: formattingoverview | CultureInfo | DateTimeFormat | DateTimeFormatInfo | DateTime.ToString

Return to top


Method: ToLongTimeString()
Summary
Converts the value of this instance to its equivalent long time string representation.
C# Syntax:
public string ToLongTimeString();
Return Value:
A string containing the name of the day of the week, the name of the month, the numeric day of the hours, minutes, and seconds equivalent to the time value of this instance.
Remarks
The value of this instance is formatted using the long time format character, 'T'. The return value is identical to the value returned by DateTime.ToString ("T", null).

For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.

This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.

Example
The following sample demonstrates DateTime.ToLongTimeString.
			System.DateTime dTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

			// If the current culture is "us-EN",
			// longDateString gets "Wednesday, May 16, 2001"
			string longDateString = dTime.ToLongDateString();

			// If the current culture is "us-EN",
			// longTimeString gets "3:02:15 AM"
			string longTimeString = dTime.ToLongTimeString();			

    
See also:
String | CultureInfo | DateTimeFormat | Calendar | DateTimeFormatInfo | DateTime.ToString

Return to top


Method: ToOADate()
Summary
Converts the value of this instance to the equivalent OLE Automation date.
C# Syntax:
public double ToOADate();
Return Value:
A double-precision floating-point number that contains an OLE Automation date equivalent to the value of this instance.
Exceptions
Exception Type Condition
OverflowException The value of this instance cannot be represented as an OLE Automation Date.
Remarks
An OLE Automation date is implemented as a floating-point number whose value is the number of days from midnight, 30 December 1899. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 represented by -1.0; and 6 A.M., 29 December 1899 represented by -1.25.

Only DateTime objects with a tick value that is greater than or equal to positive or negative 31241376000000000 can be represented as an OLE Automation Date. An uninitialized DateTime, that is, an instance with a tick value of 0, is converted to the equivalent uninitialized OLE Automation Date, that is, a date with a value of 0.0 which represents midnight, 30 December 1899.



Note The DateTime.Ticks value of a DateTime is between DateTime.MinValue and DateTime.MaxValue.

Note The maximum value of a DateTime is equal to the maximum value of an OLE Automation Date. See DateTime.FromOADate and the MSDN Online Libary at http://msdn.microsoft.com/library/default.asp for more information about OLE Automation.
See also:
Double | DateTime.FromOADate

Return to top


Method: ToShortDateString()
Summary
Converts the value of this instance to its equivalent short date string representation.
C# Syntax:
public string ToShortDateString();
Return Value:
A string containing the numeric month, the numeric day of the month, and the year equivalent to the date value of this instance.
Remarks
The value of this instance is formatted using the short date format character, 'd'. The return value is identical to the value returned by DateTime.ToString ("d", null).

For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.

This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.

See also:
MSDN: formattingoverview | CultureInfo | DateTimeFormat | DateTimeFormatInfo | DateTime.ToString

Return to top


Method: ToShortTimeString()
Summary
Converts the value of this instance to its equivalent short time string representation.
C# Syntax:
public string ToShortTimeString();
Return Value:
A string containing the name of the day of the week, the name of the month, the numeric day of the hours, minutes, and seconds equivalent to the time value of this instance.
Remarks
The value of this instance is formatted using the short time format character, 't'. The return value is identical to the value returned by DateTime.ToString ("t", null).

For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.

This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.

See also:
String | CultureInfo | DateTimeFormat | Calendar | DateTimeFormatInfo | DateTime.ToString

Return to top


Overloaded Method: ToString()
Summary
Converts the value of this instance to its equivalent string.
C# Syntax:
public override string ToString();
Return Value:
A string representation of value of this instance.
Remarks
The value of this instance is formatted using the general format specifier, 'G', as described in the the conceptual topic at MSDN: formattingoverview topic. The return value is identical to the value returned by DateTime.ToString ("G", null).

This method uses formatting information derived from the current culture. For more information, see CultureInfo.CurrentCulture.

Example
The following sample demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.
 using System;
 using System.Globalization; 
 
public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }
  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
    }
}

    
See also:
DateTimeFormatInfo | CultureInfo

Return to top


Overloaded Method: ToString(
   IFormatProvider provider
)
Summary
Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.
C# Syntax:
public string ToString(
   IFormatProvider provider
);
Parameters:

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value:
A string representation of value of this instance as specified by provider.
Remarks
This instance is formatted with the general format specifier, 'G', as described in the the conceptual topic at MSDN: formattingoverview topic. The return value is identical to the value returned by DateTime.ToString ("G", null).

The provider parameter is typically an instance of DateTimeFormatInfo. If provider is null, the DateTimeFormatInfo associated with the current culture is used. For more information, see CultureInfo.CurrentCulture.

Example
The following sample demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.
 using System;
 using System.Globalization; 
 
public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }
  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
    }
}

    
See also:
DateTimeFormatInfo | CultureInfo

Return to top


Overloaded Method: ToString(
   string format
)
Summary
Converts the value of this instance to its equivalent string representation using the specified format.
C# Syntax:
public string ToString(
   string format
);
Parameters:

format

A format string.

Return Value:
A string representation of value of this instance as specified by format.
Exceptions
Exception Type Condition
FormatException The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.

-or-

format does not contain a valid custom format pattern.

Remarks
The format parameter should contain either a format specifier character or a custom format pattern. For more information, see the summary page for DateTimeFormatInfo.

If format is null or an empty string, the general format specifier, 'G', is used.

This method uses formatting information derived from the current culture. For more information, see CultureInfo.CurrentCulture.

Example
The following sample demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.
 using System;
 using System.Globalization; 
 
public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }
  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
    }
}

    
See also:
DateTimeFormatInfo | CultureInfo

Return to top


Overloaded Method: ToString(
   string format,
   IFormatProvider provider
)
Summary
Converts the value of this instance to its equivalent string representation using the specified format and culture-specific format information.
C# Syntax:
public string ToString(
   string format,
   IFormatProvider provider
);
Parameters:

format

A format string.

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value:
A string representation of value of this instance as specified by format and provider.
Exceptions
Exception Type Condition
FormatException The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.

-or-

format does not contain a valid custom format pattern.

Remarks
The format parameter can contain either a format specifier character or a custom format pattern. For more information, see the summary page for DateTimeFormatInfo.

If format is null or an empty string (""), the standard format specifier, 'G', is used.

The provider parameter is typically an instance of DateTimeFormatInfo. If provider is null, the DateTimeFormatInfo associated with the current culture is used. For more information, see CultureInfo.CurrentCulture.

Example
The following sample demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.
 using System;
 using System.Globalization; 
 
public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }
  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
    }
}

    
See also:
DateTimeFormatInfo | CultureInfo

Return to top


Method: ToUniversalTime()
Summary
Converts the current local time to coordinated universal time (UTC).
C# Syntax:
public DateTime ToUniversalTime();
Return Value:
The UTC DateTime equivalent to the current local time.

-or-

DateTime.MaxValue if the current local time is too large to be represented as a DateTime.

-or-

DateTime.MinValue if the current local time is too small to be represented as a DateTime.

Remarks
The UTC time is equal to the local time minus the UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset.

This method assumes that the current DateTime holds the local time value, and not a UTC time. Therefore, each time it is run, the current method performs the necessary modifications on the DateTime to derive the UTC time, whether the current DateTime holds the local time or not.

This method always uses the local time zone when making calculations.

Example
The following sample demonstrates DateTime.ToUniversalTime.
			System.Console.WriteLine("Enter a date and time.");
			string strDateTime = System.Console.ReadLine();

			System.DateTime localDateTime;
			try {
				localDateTime = System.DateTime.Parse(strDateTime);
			}
			catch (System.FormatException) {
				System.Console.WriteLine("Invalid format.");
				return;
			}

			System.DateTime univDateTime = localDateTime.ToUniversalTime();

			System.Console.WriteLine("{0} local time is {1} universal time.",
									 localDateTime,
									 univDateTime); 
			
			System.Console.WriteLine("Enter a date and time in universal time.");
			strDateTime = System.Console.ReadLine();

			try {
				univDateTime = System.DateTime.Parse(strDateTime);
			}
			catch (System.FormatException) {
				System.Console.WriteLine("Invalid format.");
				return;
			}

			localDateTime = univDateTime.ToLocalTime();

			System.Console.WriteLine("{0} universal time is {1} local time.",
									 univDateTime,
									 localDateTime); 

    
See also:
DateTime.ToLocalTime

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.