System.DateTime Structure

public struct DateTime : IComparable, IFormattable, IComparable<DateTime>, IEquatable<DateTime>

Base Types

Object
  ValueType
    DateTime

This type implements IComparable, IFormattable, System.IComparable<System.DateTime>, and System.IEquatable<System.DateTime>.

Assembly

mscorlib

Library

BCL

Summary

Represents an instant in time, expressed as a date and time of day.

Description

The DateTime value type represents dates and times with values ranging from 00:00:00, 1/1/0001 (Common Era) to 23:59:59 PM, 12/31/9999.

[Note: Time values are measured in 100-nanosecond units, ticks, and a particular date is the number of ticks since 12:00 Midnight, January 1, 1 in the Gregorian calendar. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 AM.

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.

12:00:00 AM is Midnight.

]

See Also

System Namespace

Members

DateTime Constructors

DateTime(int, int, int, int, int, int, int) Constructor
DateTime(int, int, int, int, int, int) Constructor
DateTime(long) Constructor
DateTime(int, int, int) Constructor

DateTime Methods

DateTime.Add Method
DateTime.AddDays Method
DateTime.AddHours Method
DateTime.AddMilliseconds Method
DateTime.AddMinutes Method
DateTime.AddMonths Method
DateTime.AddSeconds Method
DateTime.AddTicks Method
DateTime.AddYears Method
DateTime.Compare Method
DateTime.CompareTo(System.DateTime) Method
DateTime.CompareTo(System.Object) Method
DateTime.DaysInMonth Method
DateTime.Equals(System.DateTime) Method
DateTime.Equals(System.Object) Method
DateTime.Equals(System.DateTime, System.DateTime) Method
DateTime.GetHashCode Method
DateTime.IsLeapYear Method
DateTime.Parse(System.String) Method
DateTime.Parse(System.String, System.IFormatProvider) Method
DateTime.Parse(System.String, System.IFormatProvider, System.Globalization.DateTimeStyles) Method
DateTime.ParseExact(System.String, System.String, System.IFormatProvider, System.Globalization.DateTimeStyles) Method
DateTime.ParseExact(System.String, System.String[], System.IFormatProvider, System.Globalization.DateTimeStyles) Method
DateTime.ParseExact(System.String, System.String, System.IFormatProvider) Method
DateTime.Subtract(System.TimeSpan) Method
DateTime.Subtract(System.DateTime) Method
DateTime.ToLocalTime Method
DateTime.ToLongDateString Method
DateTime.ToLongTimeString Method
DateTime.ToShortDateString Method
DateTime.ToShortTimeString Method
DateTime.ToString(System.String) Method
DateTime.ToString() Method
DateTime.ToString(System.String, System.IFormatProvider) Method
DateTime.ToString(System.IFormatProvider) Method
DateTime.ToUniversalTime Method
DateTime.op_Addition Method
DateTime.op_Equality Method
DateTime.op_GreaterThan Method
DateTime.op_GreaterThanOrEqual Method
DateTime.op_Inequality Method
DateTime.op_LessThan Method
DateTime.op_LessThanOrEqual Method
DateTime.op_Subtraction(System.DateTime, System.TimeSpan) Method
DateTime.op_Subtraction(System.DateTime, System.DateTime) Method

DateTime Fields

DateTime.MaxValue Field
DateTime.MinValue Field

DateTime Properties

DateTime.Date Property
DateTime.Day Property
DateTime.DayOfYear Property
DateTime.Hour Property
DateTime.Millisecond Property
DateTime.Minute Property
DateTime.Month Property
DateTime.Now Property
DateTime.Second Property
DateTime.Ticks Property
DateTime.TimeOfDay Property
DateTime.Today Property
DateTime.UtcNow Property
DateTime.Year Property


DateTime(int, int, int, int, int, int, int) Constructor

public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);

Summary

Constructs and initializes a new instance of the DateTime structure with a specified year, month, day, hour, minute, second, and millisecond.

Parameters

year
A Int32 containing the year (1 through 9999).
month
A Int32 containing the month (1 through 12).
day
A Int32 containing the day (1 through the number of days in month).
hour
A Int32 containing the hours (0 through 23).
minute
A Int32 containing the minutes (0 through 59).
second
A Int32 containing the seconds (0 through 59).
millisecond
A Int32 containing the milliseconds.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionyear 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

-or-

millisecond is less than 0 or greater than 999

ArgumentExceptionThe specified parameters evaluate to a date less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

See Also

System.DateTime Structure, System Namespace

DateTime(int, int, int, int, int, int) Constructor

public DateTime(int year, int month, int day, int hour, int minute, int second);

Summary

Constructs and initializes a new instance of the DateTime structure with a specified year, month, day, hour, minute, and second.

Parameters

year
A Int32 containing the year (1 through 9999).
month
A Int32 containing the month (1 through 12).
day
A Int32 containing the day (1 through the number of days in month).
hour
A Int32 containing the hours (0 through 23).
minute
A Int32 containing the minutes (0 through 59).
second
A Int32 containing the seconds (0 through 59).

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionyear 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

See Also

System.DateTime Structure, System Namespace

DateTime(long) Constructor

public DateTime(long ticks);

Summary

Constructs and initializes a new instance of the DateTime structure with the date and time expressed in 100-nanosecond units.

Parameters

ticks
A Int64containing the date and time expressed in 100-nanosecond units.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe date and time represented by ticks is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

See Also

System.DateTime Structure, System Namespace

DateTime(int, int, int) Constructor

public DateTime(int year, int month, int day);

Summary

Constructs and initializes a new instance of the DateTime structure with a specified year, month, and day.

Parameters

year
A Int32 containing the year (1 through 9999).
month
A Int32 containing the month (1 through 12).
day
A Int32 containing the day (1 through the number of days in month).

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionyear 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

Description

The time of day for the resulting DateTime is midnight (00:00:00).

See Also

System.DateTime Structure, System Namespace

DateTime.Add Method

public DateTime Add(TimeSpan value);

Summary

Adds the value of a specified TimeSpan instance to the current instance.

Parameters

value
A TimeSpan instance.

Return Value

A DateTime instance set to the sum of the date and time of the current instance and the time interval represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

A specified TimeSpan is added to the current instance of DateTime, and the result is returned as a new DateTime.

See Also

System.DateTime Structure, System Namespace

DateTime.AddDays Method

public DateTime AddDays(double value);

Summary

Adds a specified number of days to the value of the current instance.

Parameters

value
A Double containing the number of whole and fractional days. For example, 4.5 is equivalent to 4 days, 12 hours, 0 minutes, 0 seconds, 0 milliseconds, and 0 ticks. value can be negative or positive.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the number of days represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

[Note: value is rounded to the nearest tick.]

Library

ExtendedNumerics

See Also

System.DateTime Structure, System Namespace

DateTime.AddHours Method

public DateTime AddHours(double value);

Summary

Adds a specified number of hours to the value of the current instance.

Parameters

value
A Double containing the number of whole and fractional hours. For example, 4.5 is equivalent to 4 hours, 30 minutes, 0 seconds, 0 milliseconds, and 0 ticks. value can be negative or positive.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the number of hours represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

[Note: value is rounded to the nearest tick.]

Library

ExtendedNumerics

See Also

System.DateTime Structure, System Namespace

DateTime.AddMilliseconds Method

public DateTime AddMilliseconds(double value);

Summary

Adds a specified number of milliseconds to the value of the current instance.

Parameters

value
A Double containing the number of whole and fractional milliseconds. For example, 4.5 is equivalent to 4 milliseconds and 5,000 ticks. value can be negative or positive.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the number of milliseconds represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

[Note: value is rounded to the nearest tick.]

Library

ExtendedNumerics

See Also

System.DateTime Structure, System Namespace

DateTime.AddMinutes Method

public DateTime AddMinutes(double value);

Summary

Adds a specified number of minutes to the value of the current instance.

Parameters

value
A Double containing the number of whole and fractional minutes. For example, 4.5 is equivalent to 4 minutes, 30 seconds, 0 milliseconds, and 0 ticks. value can be negative or positive.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the number of minutes represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

[Note: value is rounded to the nearest tick.]

Library

ExtendedNumerics

See Also

System.DateTime Structure, System Namespace

DateTime.AddMonths Method

public DateTime AddMonths(int months);

Summary

Adds a specified number of months to the value of the current instance.

Parameters

months
A Int32 containing the number of months. months can be positive or negative, and can be greater than the number of months in a year.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and months.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

-or-

The months parameter is less than -120,000 or greater than 120,000

Description

This method does not change the value of the current DateTime instance. Instead, a new DateTime instance is returned whose value is the result of this operation.

See Also

System.DateTime Structure, System Namespace

DateTime.AddSeconds Method

public DateTime AddSeconds(double value);

Summary

Adds a specified number of seconds to the value of the current instance.

Parameters

value
A Double containing the number of whole and fractional seconds. For example, 4.5 is equivalent to 4 seconds, 500 milliseconds, and 0 ticks. value can be positive or negative.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the number of seconds represented by value.

Exceptions

Exception TypeCondition
ArgumentExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

[Note: value is rounded to the nearest tick.]

Library

ExtendedNumerics

See Also

System.DateTime Structure, System Namespace

DateTime.AddTicks Method

public DateTime AddTicks(long value);

Summary

Adds a specified number of ticks to the value of the current instance.

Parameters

value
A Int64 containing the number of 100-nanosecond ticks. value can be positive or negative.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the time represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

See Also

System.DateTime Structure, System Namespace

DateTime.AddYears Method

public DateTime AddYears(int value);

Summary

Adds a specified number of years to the value of the current instance.

Parameters

value
A Int32 containing the number of years. value can be positive or negative.

Return Value

A DateTime instance set to the sum of the date and time represented by the current instance and the number of years represented by value.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

See Also

System.DateTime Structure, System Namespace

DateTime.Compare Method

public static int Compare(DateTime t1, DateTime t2);

Summary

Returns the sort order of the two specified instances of DateTime .

Parameters

t1
The first DateTime.
t2
The second DateTime.

Return Value

The return value is a negative number, zero, or a positive number reflecting the sort order of the two specified instances of DateTime. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

Value TypeCondition
Any negative numbert1 < t2.
Zerot1 == t2.
Any positive numbert1 > t2 .

See Also

System.DateTime Structure, System Namespace

DateTime.CompareTo(System.DateTime) Method

public int CompareTo(DateTime value);

Summary

Returns the sort order of the current instance compared to the specified DateTime .

Parameters

value
The DateTime to compare to the current instance.

Return Value

The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

ValueDescription
A negative numberCurrent instance < value.
ZeroCurrent instance == value.
A positive numberCurrent instance > value.

Description

[Note: This method is implemented to support the System.IComparable<System.DateTime> interface.]

See Also

System.DateTime Structure, System Namespace

DateTime.CompareTo(System.Object) Method

public int CompareTo(object value);

Summary

Returns the sort order of the current instance compared to the specified Object .

Parameters

value
The Object to compare to the current instance.

Return Value

The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

ValueDescription
A negative numberCurrent instance < value.
ZeroCurrent instance == value.
A positive numberCurrent instance > value, or value is a null reference.

Exceptions

Exception TypeCondition
ArgumentExceptionvalue is not a DateTime and is not a null reference.

Description

Any instance of DateTime, regardless of its value, is considered greater than a null reference.

[Note: This method is implemented to support the IComparable interface.]

See Also

System.DateTime Structure, System Namespace

DateTime.DaysInMonth Method

public static int DaysInMonth(int year, int month);

Summary

Returns the number of days in a specified month of a specified year.

Parameters

year
A Int32 containing the year.
month
The month (a Int32 between 1 and 12).

Return Value

A Int32 set to the number of days in the specified month for the specified year. If the specified month is February, the return value is 28 or 29 depending upon whether the specified year is a leap year.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionmonth is less than 1 or greater than 12.

See Also

System.DateTime Structure, System Namespace

DateTime.Equals(System.DateTime) Method

public override bool Equals(DateTime value);

Summary

Returns a Boolean indicating whether the current instance is equal to the specified DateTime.

Parameters

value
A DateTime to compare with the current instance.

Return Value

true if value is equal to the current instance; otherwise, false .

Description

[Note: This method is implemented to support the System.IEquatable<System.DateTime> interface.]

See Also

System.DateTime Structure, System Namespace

DateTime.Equals(System.Object) Method

public override bool Equals(object value);

Summary

Returns a Boolean indicating whether the current instance is equal to a specified object.

Parameters

value
A Object to compare with the current instance.

Return Value

true if value is a specified DateTime instance is equal to the current instance; otherwise, false .

Description

[Note: This method overrides System.Object.Equals(System.Object).]

See Also

System.DateTime Structure, System Namespace

DateTime.Equals(System.DateTime, System.DateTime) Method

public static bool Equals(DateTime t1, DateTime t2);

Summary

Returns a Boolean indicating whether two specified instances of DateTime are equal.

Parameters

t1
The first DateTime.
t2
The second DateTime.

Return Value

true if the two DateTime values are equal; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.GetHashCode Method

public override int GetHashCode();

Summary

Generates a hash code for the current instance.

Return Value

A Int32 containing the hash code for this instance.

Description

The algorithm used to generate the hash code is unspecified.

[Note: This method overrides System.Object.GetHashCode.]

See Also

System.DateTime Structure, System Namespace

DateTime.IsLeapYear Method

public static bool IsLeapYear(int year);

Summary

Returns a Boolean value indicating whether a specified year is a leap year.

Parameters

year
A Int32 representing the year. year can be positive or negative.

Return Value

true if the specified year is a leap year; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.Parse(System.String) Method

public static DateTime Parse(string s);

Summary

Returns the specified String converted to a DateTime value.

Parameters

s
A String containing a value to convert. The string is interpreted using the System.Globalization.DateTimeStyles.None style.

Return Value

The DateTime value obtained from s.

Exceptions

Exception TypeCondition
ArgumentNullExceptions is a null reference.
FormatExceptions does not contain a valid string representation of a time or date and time.

Description

This version of System.DateTime.Parse(System.String) is equivalent to System.DateTime.Parse(System.String)(s, null , System.Globalization.DateTimeStyles.None ).

The string s is parsed using the formatting information in a DateTimeFormatInfo initialized for the current system culture.

In order for the string to be successfully parsed, it is required to represent a date and time value in one of the standard DateTime patterns described in DateTimeFormatInfo .

If the string contains only a time, and no date, then the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m.

Any leading, trailing, and inner white space characters are ignored.

See Also

System.DateTime Structure, System Namespace

DateTime.Parse(System.String, System.IFormatProvider) Method

public static DateTime Parse(string s, IFormatProvider provider);

Summary

Returns the specified String converted to a DateTime value.

Parameters

s
A String containing the value to convert. The string is interpreted using the System.Globalization.DateTimeStyles.None style.
provider
A IFormatProvider that supplies a DateTimeFormatInfo object containing culture-specific format information about s .

Return Value

The DateTime value obtained from s.

Exceptions

Exception TypeCondition
ArgumentExceptions is a null reference.
FormatExceptions does not contain a valid string representation of a time or date and time.

Description

This version of System.DateTime.Parse(System.String) is equivalent to System.DateTime.Parse(System.String)(s, provider, System.Globalization.DateTimeStyles.None ).

The string s is parsed using the culture-specific formatting information from the DateTimeFormatInfo instance supplied by provider. If provider is null or a DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

In order for the string to be successfully parsed, it is required to represent a date and time value in one of the standard DateTime patterns described in DateTimeFormatInfo .

If the string contains only a time, and no date, then the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m.

Any leading, trailing, and inner white space characters are ignored.

See Also

System.DateTime Structure, System Namespace

DateTime.Parse(System.String, System.IFormatProvider, System.Globalization.DateTimeStyles) Method

public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles styles);

Summary

Returns the specified String converted to a DateTime value.

Parameters

s
A String containing the value to convert.
provider
A IFormatProvider that supplies a DateTimeFormatInfo object containing culture-specific format information about s .
styles
One or more DateTimeStyles values that specify the style of s. Specify multiple values for styles using the bitwise OR operator.

Return Value

The DateTime value obtained from s.

Exceptions

Exception TypeCondition
ArgumentExceptions is a null reference.
FormatExceptions does not contain a valid string representation of a time or date and time.

Description

The string s is parsed using the culture-specific formatting information from the DateTimeFormatInfo instance supplied by provider. If provider is null or a DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

In order for the string to be successfully parsed, it is required to represent a date and time value in one of the standard DateTime patterns described in DateTimeFormatInfo .

If the string contains only a time, and no date, and if the styles parameter is set to System.Globalization.DateTimeStyles.NoCurrentDateDefault the Gregorian year 1, month 1, day 1 are used. In all other cases where a date is not specified, the current date (System.DateTime.Now ) is used.

If the string contains only a date and no time, this method assumes 12 a.m.

For all settings of the styles parameter, any leading, trailing, and inner white space characters are ignored.

See Also

System.DateTime Structure, System Namespace

DateTime.ParseExact(System.String, System.String, System.IFormatProvider, System.Globalization.DateTimeStyles) Method

public static DateTime ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style);

Summary

Converts the String representation of a date and time to its DateTime equivalent using a specified style, the expected format, and culture-specific format information.

Parameters

s
A String containing a date and time to convert. The format of the string is required to match the specified format exactly.
format
A String containing the expected format of s. [Note: For a list of valid format values, see DateTimeFormatInfo .]

provider
A IFormatProvider that supplies a DateTimeFormatInfo object containing culture-specific format information about s .
style
One or more DateTimeStyles values that specify the style of s. Specify multiple values for styles using the bitwise OR operator.

Return Value

A DateTime equivalent to the date and time contained in s.

Exceptions

Exception TypeCondition
ArgumentNullExceptions or format is a null reference.
FormatExceptions or format is an empty string.

-or-

s does not contain a date and time that were recognized as one of the patterns specified in format.

Description

System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) constructs a DateTime from the string s. The string is required to specify a date and, optionally, a time in the provided format.

The string s is parsed using the culture-specific formatting information from the DateTimeFormatInfo instance supplied by provider. If provider is null or a DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If the s string contains only a time, and no date, and if the styles parameter is set to System.Globalization.DateTimeStyles.NoCurrentDateDefault the Gregorian year 1, month 1, day 1 are used, and no leading, trailing, or inner white space characters are allowed. In all other cases where a date is not specified, the current date (System.DateTime.Now) is used.

If the s string contains only a date and no time, this method assumes 12 a.m.

[Note: For information on formatting system-supplied data types, see the IFormattable interface.]

Example

This example demonstrates the System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) method.

using System;
using System.Globalization;

public class DateTimeTest {
 public static void Main() {
     DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

     DateTime dt = DateTime.ParseExact(" January 22 ", dtfi.MonthDayPattern, null, DateTimeStyles.AllowWhiteSpaces);
     Console.WriteLine(dt);
 }
}
   
The output is

1/22/2001 12:00:00 AM

See Also

System.DateTime Structure, System Namespace

DateTime.ParseExact(System.String, System.String[], System.IFormatProvider, System.Globalization.DateTimeStyles) Method

public static DateTime ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style);

Summary

Converts the String representation of a date and time to its DateTime equivalent using a specified style, an array of expected formats, and culture-specific format information.

Parameters

s
A String containing one or more dates and times to convert. The format of the string is required to match the specified format exactly.
formats
A String array containing the expected formats of s. [Note: For a list of valid format values, see DateTimeFormatInfo .]

provider
A IFormatProvider that supplies a DateTimeFormatInfo object containing culture-specific format information about s .
style
One or more DateTimeStyles values that specify the style of s. Specify multiple values for styles using the bitwise OR operator.

Return Value

A DateTime equivalent to the date and time contained in s.

Exceptions

Exception TypeCondition
ArgumentNullExceptions or formats is a null reference.
FormatExceptions or format is an empty string.

-or-

s does not contain a date and time that were recognized as the pattern specified in format

Description

System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) constructs a DateTime from the sString. The string is required to specify a date and, optionally, a time in the provided format.

The string s is parsed using the culture-specific formatting information from the DateTimeFormatInfo instance supplied by provider. If provider is null or a DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If the s string contains only a time, and no date, and if the styles parameter is set to System.Globalization.DateTimeStyles.NoCurrentDateDefault the Gregorian year 1, month 1, day 1 are used, and no leading, trailing, or inner white space characters are allowed. In all other cases where a date is not specified, the current date (System.DateTime.Now) is used.

If the s string contains only a date and no time, this method assumes 12 a.m.

[Note: For information on formatting system-supplied data types, see the IFormattable interface.]

Example

This example demonstrates the System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) method.

using System;
using System.Globalization;

public class DateTimeTest {
 public static void Main() {
     DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
     string [] patterns = {dtfi.LongTimePattern, dtfi.ShortTimePattern}; 

     DateTime dt = DateTime.ParseExact("10:11:12", patterns, null, DateTimeStyles.NoCurrentDateDefault);
     Console.WriteLine(dt);
 }
}
   
The output is

1/1/0001 10:11:12 AM

See Also

System.DateTime Structure, System Namespace

DateTime.ParseExact(System.String, System.String, System.IFormatProvider) Method

public static DateTime ParseExact(string s, string format, IFormatProvider provider);

Summary

Converts the specified String representation of a date and time to its DateTime equivalent using a specified format and IFormatProvider .

Parameters

s
A String containing a date and time to convert. The format of the string is required to match the specified format exactly.
format
A String containing the expected format of s. [Note: For a list of valid format values, see DateTimeFormatInfo .]

provider
A IFormatProvider that supplies a DateTimeFormatInfo object containing culture-specific format information about s .

Return Value

A DateTime equivalent to the date and time contained in s.

Exceptions

Exception TypeCondition
ArgumentNullExceptions or format is a null reference.
FormatExceptions or format is an empty string.

-or-

s does not contain a date and time that were recognized as the pattern specified in format .

Description

System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) constructs a DateTime from the string s. The string is required to specify a date and, optionally, a time in the specified format.

The string s is parsed using the culture-specific formatting information from the DateTimeFormatInfo instance supplied by provider. If provider is null or a DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If the s string contains only a time, and no date, then the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m.

Leading, trailing, and inner white space characters are not allowed.

[Note: For information on formatting system-supplied data types, see the IFormattable interface.]

Example

This example demonstrates the System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) method.

using System;
using System.Globalization;

public class DateTimeTest {
 public static void Main() {
     DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

     DateTime dt = DateTime.ParseExact("January 22", dtfi.MonthDayPattern, null);
     Console.WriteLine(dt);
 }
}
   
The output is

1/22/2001 12:00:00 AM

See Also

System.DateTime Structure, System Namespace

DateTime.Subtract(System.TimeSpan) Method

public DateTime Subtract(TimeSpan value);

Summary

Subtracts a specified TimeSpan from the current instance.

Parameters

value
An instance of TimeSpan.

Return Value

A new DateTime instance equal to the date and time represented by the current instance minus the time interval of the specified TimeSpan.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

See Also

System.DateTime Structure, System Namespace

DateTime.Subtract(System.DateTime) Method

public TimeSpan Subtract(DateTime value);

Summary

Subtracts a specified date and time from the current instance.

Parameters

value
An instance of DateTime.

Return Value

A TimeSpan interval equal to the date and time represented by the current instance minus the date and time represented by the specified DateTime.

See Also

System.DateTime Structure, System Namespace

DateTime.ToLocalTime Method

public DateTime ToLocalTime();

Summary

Converts the universal time coordinate (UTC) time value in the current instance to local time.

Return Value

An instance of DateTime equivalent of the time value in the current instance, adjusted to the local time zone and daylight saving time. If the result is too large or too small to be represented as a DateTime , this method returns a DateTime set to System.DateTime.MaxValue or System.DateTime.MinValue .

Description

This method assumes that the current instance of DateTime holds the UTC time value, and not a local time. Each time it is invoked, this method performs the necessary modifications on the DateTime to derive the local time, whether the current DateTime holds the UTC time or not.

The local time zone information is obtained from the operating system.

See Also

System.DateTime Structure, System Namespace

DateTime.ToLongDateString Method

public string ToLongDateString();

Summary

Converts the date denoted by the current instance to its equivalent long date String representation.

Return Value

A String containing the same value as a String returned by System.DateTime.ToString ("D", null ).

Description

The value of the current instance is formatted using the long date format specifier, 'D'.

[Note: This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString .

For more information regarding the long date specifier, see DateTimeFormatInfo .

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToLongTimeString Method

public string ToLongTimeString();

Summary

Converts the time denoted by the current instance to its equivalent long time String representation.

Return Value

A String containing the same value as a String returned by System.DateTime.ToString ("T", null ).

Description

The value of the current instance is formatted using the long time format specifier, 'T'.

[Note: This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString .

For more information regarding the long time specifier, see DateTimeFormatInfo .

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToShortDateString Method

public string ToShortDateString();

Summary

Converts the date denoted by the current instance to its equivalent short date String representation.

Return Value

A String containing the same value as a String returned by System.DateTime.ToString ("d", null ).

Description

The value of the current instance is formatted using the short date format specifier, 'd'.

[Note: This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString .

For more information regarding the short date specifier, see DateTimeFormatInfo .

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToShortTimeString Method

public string ToShortTimeString();

Summary

Converts the time denoted by this instance to its equivalent short time String representation.

Return Value

A String containing the same value as a String returned by System.DateTime.ToString ("t", null ).

Description

The value of the current instance is formatted using the short time format specifier, 't'.

[Note: This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString .

For more information regarding the short time specifier, see DateTimeFormatInfo .

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToString(System.String) Method

public string ToString(string format);

Summary

Returns a String representation of the value of the current instance.

Parameters

format
A String that specifies the format of the returned string. [Note: For a list of valid values, see DateTimeFormatInfo .]

Return Value

A String representation of the current instance formatted as specified by format. The string takes into account the current system culture.

Exceptions

Exception TypeCondition
FormatExceptionThe length of the format string is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.

-or-

The format string does not contain a valid custom format pattern.

Description

This version of System.DateTime.ToString is equivalent to System.DateTime.ToString (format , null ).

If format is a null reference, the general format specifier "G" is used.

[Note: This method uses the culture information of the current thread.

For information on formatting system-supplied data types, see the IFormattable interface.

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToString() Method

public override string ToString();

Summary

Returns a String representation of the value of the current instance.

Return Value

A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the current system culture.

Description

This version of System.DateTime.ToString is equivalent to System.DateTime.ToString ("G", null ).

[Note: For more information about the general format specifier ("G") see DateTimeFormatInfo.

This method overrides System.Object.ToString.

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToString(System.String, System.IFormatProvider) Method

public string ToString(string format, IFormatProvider provider);

Summary

Returns a String representation of the value of the current instance.

Parameters

format
A String containing a character that specifies the format of the returned string. [Note: For a list of valid values, see DateTimeFormatInfo .]

provider
A IFormatProvider that supplies a DateTimeFormatInfo instance containing culture-specific formatting information.

Return Value

A String representation of the current instance formatted as specified by format. The string takes into account the information in the DateTimeFormatInfo supplied by provider.

Exceptions

Exception TypeCondition
FormatExceptionThe length of the format string is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.

-or-

The format string does not contain a valid custom format pattern.

Description

If provider is null or a DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If format is a null reference, the general format specifier "G" is used.

[Note: For more information regarding the standard format specifier, see DateTimeFormatInfo. For information on formatting system-supplied data types, see the IFormattable interface.

This method is implemented to support the IFormattable interface.

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToString(System.IFormatProvider) Method

public string ToString(IFormatProvider provider);

Summary

Returns a String representation of the value of the current instance.

Parameters

provider
A IFormatProvider that supplies a DateTimeFormatInfo containing culture-specific formatting information.

Return Value

A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the formatting information in the DateTimeFormatInfo instance supplied by provider.

Description

This version of System.DateTime.ToString is equivalent to System.DateTime.ToString ("G", provider ).

If provider is null or the DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

[Note: The general format specifier ("G") provides the general date pattern including the long time form, equivalent to System.Globalization.DateTimeFormatInfo.ShortDatePattern combined with System.Globalization.DateTimeFormatInfo.LongTimePattern. For more information on format specifiers, see DateTimeFormatInfo . For information on formatting system-supplied data types, see the IFormattable interface.

]

See Also

System.DateTime Structure, System Namespace

DateTime.ToUniversalTime Method

public DateTime ToUniversalTime();

Summary

Converts the current DateTime value to coordinated universal time (UTC).

Return Value

The UTC DateTime equivalent of the current DateTime value. If the result is too large or too small to be represented as a DateTime, the current function returns a DateTime set to System.DateTime.MaxValue or System.DateTime.MinValue .

Description

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

The local time zone information is obtained from the operating system.

See Also

System.DateTime Structure, System Namespace

DateTime.op_Addition Method

public static DateTime operator +(DateTime d, TimeSpan t);

Summary

Adds a specified TimeSpan value to a specified DateTime value.

Parameters

d
A DateTime value.
t
A TimeSpan value.

Return Value

A DateTime instance that is the sum of the values of d and t .

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

The returned value is equivalent to DateTime ( d.Ticks + t.Ticks ).

See Also

System.DateTime Structure, System Namespace

DateTime.op_Equality Method

public static bool operator ==(DateTime d1, DateTime d2);

Summary

Returns a Boolean value indicating whether the two specified instances of DateTime are equal.

Parameters

d1
The first DateTime to compare.
d2
The second DateTime to compare.

Return Value

true if d1.Ticks value is equal to the d2.Ticks value; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.op_GreaterThan Method

public static bool operator >(DateTime t1, DateTime t2);

Summary

Returns a Boolean value indicating whether one specified DateTime is greater than another specified DateTime.

Parameters

t1
A DateTime.

t2
A DateTime.

Return Value

true if t1.Ticks value is greater than the t2.Ticks value; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.op_GreaterThanOrEqual Method

public static bool operator >=(DateTime t1, DateTime t2);

Summary

Returns a Boolean value indicating whether one specified DateTime is greater than or equal to another specified DateTime.

Parameters

t1
A DateTime.

t2
A DateTime.

Return Value

true if t1.Ticks value is greater than or equal to t2.Ticks value; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.op_Inequality Method

public static bool operator !=(DateTime d1, DateTime d2);

Summary

Returns a Boolean value indicating whether two specified instances of DateTime are not equal.

Parameters

d1
A DateTime.
d2
A DateTime.

Return Value

true if d1.Ticks value is not equal to d2.Ticks value; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.op_LessThan Method

public static bool operator  <(DateTime t1, DateTime t2);

Summary

Returns a Boolean value indicating whether one specified DateTime is less than another specified DateTime.

Parameters

t1
A DateTime.

t2
A DateTime.

Return Value

true if t1.Ticks value is less than t2.Ticks value; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.op_LessThanOrEqual Method

public static bool operator <=(DateTime t1, DateTime t2);

Summary

Returns a Boolean value indicating whether one specified DateTime is less than or equal to another specified DateTime.

Parameters

t1
A DateTime.

t2
A DateTime.

Return Value

true if t1.Ticks value is less than or equal to t2.Ticks value; otherwise, false .

See Also

System.DateTime Structure, System Namespace

DateTime.op_Subtraction(System.DateTime, System.TimeSpan) Method

public static DateTime operator -(DateTime d, TimeSpan t);

Summary

Subtracts a specified TimeSpan from a specified DateTime .

Parameters

d
A DateTime.
t
A TimeSpan.

Return Value

A DateTime whose value is the value of d minus the value of t .

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

The returned value is equivalent to DateTime( d.Ticks - t.Ticks ).

See Also

System.DateTime Structure, System Namespace

DateTime.op_Subtraction(System.DateTime, System.DateTime) Method

public static TimeSpan operator -(DateTime d1, DateTime d2);

Summary

Subtracts a specified DateTime from another specified DateTime value, producing a time interval.

Parameters

d1
A DateTime (the minuend).
d2
A DateTime (the subtrahend).

Return Value

A TimeSpan that is the time interval between d1 and d2.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.

Description

The returned value is equivalent to TimeSpan( d1.Ticks - d2.Ticks).

See Also

System.DateTime Structure, System Namespace

DateTime.MaxValue Field

public static readonly DateTime MaxValue;

Summary

A constant representing the largest possible value of DateTime.

Description

This field is read-only.

The value of this field is equivalent to 23:59:59.9999999, 12/31/9999, exactly one 100-nanosecond tick before 00:00:00, 01/01/10000.

See Also

System.DateTime Structure, System Namespace

DateTime.MinValue Field

public static readonly DateTime MinValue;

Summary

A constant representing the smallest possible value of DateTime.

Description

This field is read-only.

The value of this field is equivalent to 00:00:00.0000000, 1/1/0001.

See Also

System.DateTime Structure, System Namespace

DateTime.Date Property

public DateTime Date { get; }

Summary

Gets the date component of the current instance.

Property Value

A new DateTime instance with the same date as the current instance, and the time value set to midnight (00:00:00).

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Day Property

public int Day { get; }

Summary

Gets the day of the month represented by the current instance.

Property Value

A Int32 between 1 and 31 set to the day of the month component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.DayOfYear Property

public int DayOfYear { get; }

Summary

Gets the day of the year represented by the current instance.

Property Value

A Int32 between 1 and 366 set to the day of the year component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Hour Property

public int Hour { get; }

Summary

Gets the hour represented by the current instance.

Property Value

A Int32 between 0 and 23 set to the hour component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Millisecond Property

public int Millisecond { get; }

Summary

Gets the milliseconds component of the date represented by the current instance.

Property Value

A Int32 between 0 and 999 set to the milliseconds component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Minute Property

public int Minute { get; }

Summary

Gets the minute component of the date represented by the current instance.

Property Value

A Int32 between 0 and 59 set to the minute component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Month Property

public int Month { get; }

Summary

Gets the month component of the date represented by the current instance.

Property Value

A Int32 between 1 and 12 set to the month component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Now Property

public static DateTime Now { get; }

Summary

Gets a DateTime representing the current local date and time.

Description

The resolution of this property depends on the system timer.

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Second Property

public int Second { get; }

Summary

Gets the seconds component of the date represented by the current instance.

Property Value

A Int32 between 0 and 59 set to the seconds component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Ticks Property

public long Ticks { get; }

Summary

Gets the number of 100-nanosecond ticks that represent the date and time of the current instance.

Property Value

A Int64 set to the number of ticks that represent the date and time of the current instance.

Description

The value of this property is the number of 100-nanosecond intervals that have elapsed since 00:00:00, 1/1/0001. The value of the property is between System.DateTime.MinValue and System.DateTime.MaxValue .

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.TimeOfDay Property

public TimeSpan TimeOfDay { get; }

Summary

Gets the time of day of the current instance.

Property Value

A TimeSpan instance set to the time component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Today Property

public static DateTime Today { get; }

Summary

Gets the current date.

Property Value

A DateTime instance set to the date of the current instance, with the time set to 00:00:00.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.UtcNow Property

public static DateTime UtcNow { get; }

Summary

Gets the current time converted to coordinated universal time (UTC).

Property Value

A DateTime instance set to the current date and time in coordinated universal time (UTC).

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace

DateTime.Year Property

public int Year { get; }

Summary

Gets the year component of the date represented by the current instance.

Property Value

A Int32 between 1 and 9999 set to the year component of the current instance.

Description

This property is read-only.

See Also

System.DateTime Structure, System Namespace