public struct DateTime : IComparable, IFormattable, IComparable<DateTime>, IEquatable<DateTime>
Object
ValueType
DateTimeThis type implements IComparable, IFormattable, System.IComparable<System.DateTime>, and System.IEquatable<System.DateTime>.
mscorlib
BCL
Represents an instant in time, expressed as a date and time of day.
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.
]
System Namespace
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
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
Constructs and initializes a new instance of the DateTime structure with a specified year, month, day, hour, minute, second, and millisecond.
- 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.
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
-or-
millisecond is less than 0 or greater than 999
ArgumentException The specified parameters evaluate to a date less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
System.DateTime Structure, System Namespace
public DateTime(int year, int month, int day, int hour, int minute, int second);
Constructs and initializes a new instance of the DateTime structure with a specified year, month, day, hour, minute, and second.
- 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).
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
System.DateTime Structure, System Namespace
public DateTime(long ticks);
Constructs and initializes a new instance of the DateTime structure with the date and time expressed in 100-nanosecond units.
- ticks
- A Int64containing the date and time expressed in 100-nanosecond units.
Exception Type Condition ArgumentOutOfRangeException The date and time represented by ticks is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
System.DateTime Structure, System Namespace
public DateTime(int year, int month, int day);
Constructs and initializes a new instance of the DateTime structure with a specified year, month, and day.
- 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).
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
The time of day for the resulting DateTime is midnight (00:00:00).
System.DateTime Structure, System Namespace
public DateTime Add(TimeSpan value);
Adds the value of a specified TimeSpan instance to the current instance.
- value
- A TimeSpan instance.
A DateTime instance set to the sum of the date and time of the current instance and the time interval represented by value.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
A specified TimeSpan is added to the current instance of DateTime, and the result is returned as a new DateTime.
System.DateTime Structure, System Namespace
public DateTime AddDays(double value);
Adds a specified number of days to the value of the current instance.
- 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.
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.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
[Note: value is rounded to the nearest tick.]
ExtendedNumerics
System.DateTime Structure, System Namespace
public DateTime AddHours(double value);
Adds a specified number of hours to the value of the current instance.
- 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.
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.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
[Note: value is rounded to the nearest tick.]
ExtendedNumerics
System.DateTime Structure, System Namespace
public DateTime AddMilliseconds(double value);
Adds a specified number of milliseconds to the value of the current instance.
- 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.
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.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
[Note: value is rounded to the nearest tick.]
ExtendedNumerics
System.DateTime Structure, System Namespace
public DateTime AddMinutes(double value);
Adds a specified number of minutes to the value of the current instance.
- 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.
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.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
[Note: value is rounded to the nearest tick.]
ExtendedNumerics
System.DateTime Structure, System Namespace
public DateTime AddMonths(int months);
Adds a specified number of months to the value of the current instance.
- 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.
A DateTime instance set to the sum of the date and time represented by the current instance and months.
Exception Type Condition ArgumentOutOfRangeException The 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
This method does not change the value of the currentDateTime
instance. Instead, a newDateTime
instance is returned whose value is the result of this operation.
System.DateTime Structure, System Namespace
public DateTime AddSeconds(double value);
Adds a specified number of seconds to the value of the current instance.
- 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.
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.
Exception Type Condition ArgumentException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
[Note: value is rounded to the nearest tick.]
ExtendedNumerics
System.DateTime Structure, System Namespace
public DateTime AddTicks(long value);
Adds a specified number of ticks to the value of the current instance.
- value
- A Int64 containing the number of 100-nanosecond ticks. value can be positive or negative.
A DateTime instance set to the sum of the date and time represented by the current instance and the time represented by value.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
System.DateTime Structure, System Namespace
public DateTime AddYears(int value);
Adds a specified number of years to the value of the current instance.
- value
- A Int32 containing the number of years. value can be positive or negative.
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.
Exception Type Condition ArgumentOutOfRangeException The resulting DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
System.DateTime Structure, System Namespace
public static int Compare(DateTime t1, DateTime t2);
Returns the sort order of the two specified instances of DateTime .
- t1
- The first DateTime.
- t2
- The second DateTime.
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 Type Condition Any negative number t1 < t2. Zero t1 == t2. Any positive number t1 > t2 .
System.DateTime Structure, System Namespace
public int CompareTo(DateTime value);
Returns the sort order of the current instance compared to the specified DateTime .
- value
- The DateTime to compare to the current instance.
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:
Value Description A negative number Current instance < value. Zero Current instance == value. A positive number Current instance > value.
[Note: This method is implemented to support the System.IComparable<System.DateTime> interface.]
System.DateTime Structure, System Namespace
public int CompareTo(object value);
Returns the sort order of the current instance compared to the specified Object .
- value
- The Object to compare to the current instance.
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:
Value Description A negative number Current instance < value. Zero Current instance == value. A positive number Current instance > value, or value is a null reference.
Exception Type Condition ArgumentException value is not a DateTime and is not a null reference.
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.]
System.DateTime Structure, System Namespace
public static int DaysInMonth(int year, int month);
Returns the number of days in a specified month of a specified year.
- year
- A Int32 containing the year.
- month
- The month (a Int32 between 1 and 12).
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.
Exception Type Condition ArgumentOutOfRangeException month is less than 1 or greater than 12.
System.DateTime Structure, System Namespace
public override bool Equals(DateTime value);
Returns a Boolean indicating whether the current instance is equal to the specified DateTime.
- value
- A DateTime to compare with the current instance.
true
if value is equal to the current instance; otherwise,false
.
[Note: This method is implemented to support the System.IEquatable<System.DateTime> interface.]
System.DateTime Structure, System Namespace
public override bool Equals(object value);
Returns a Boolean indicating whether the current instance is equal to a specified object.
- value
- A Object to compare with the current instance.
true
if value is a specified DateTime instance is equal to the current instance; otherwise,false
.
[Note: This method overrides System.Object.Equals(System.Object).]
System.DateTime Structure, System Namespace
public static bool Equals(DateTime t1, DateTime t2);
Returns a Boolean indicating whether two specified instances of DateTime are equal.
- t1
- The first DateTime.
- t2
- The second DateTime.
true
if the two DateTime values are equal; otherwise,false
.
System.DateTime Structure, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for this instance.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode.]
System.DateTime Structure, System Namespace
public static bool IsLeapYear(int year);
Returns a Boolean value indicating whether a specified year is a leap year.
- year
- A Int32 representing the year. year can be positive or negative.
true
if the specified year is a leap year; otherwise,false
.
System.DateTime Structure, System Namespace
public static DateTime Parse(string s);
Returns the specified String converted to a DateTime value.
- s
- A String containing a value to convert. The string is interpreted using the System.Globalization.DateTimeStyles.None style.
The DateTime value obtained from s.
Exception Type Condition ArgumentNullException s is a null reference. FormatException s does not contain a valid string representation of a time or date and time.
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.
System.DateTime Structure, System Namespace
public static DateTime Parse(string s, IFormatProvider provider);
Returns the specified String converted to a DateTime value.
- 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 .
The DateTime value obtained from s.
Exception Type Condition ArgumentException s is a null reference. FormatException s does not contain a valid string representation of a time or date and time.
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.
System.DateTime Structure, System Namespace
public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles styles);
Returns the specified String converted to a DateTime value.
- 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.
The DateTime value obtained from s.
Exception Type Condition ArgumentException s is a null reference. FormatException s does not contain a valid string representation of a time or date and time.
The string s is parsed using the culture-specific formatting information from the DateTimeFormatInfo instance supplied by provider. If provider isnull
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.
System.DateTime Structure, System Namespace
public static DateTime ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style);
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.
- 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.
A DateTime equivalent to the date and time contained in s.
Exception Type Condition ArgumentNullException s or format is a null reference. FormatException s 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.
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.]
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
System.DateTime Structure, System Namespace
public static DateTime ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style);
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.
- 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.
A DateTime equivalent to the date and time contained in s.
Exception Type Condition ArgumentNullException s or formats is a null reference. FormatException s or format is an empty string. -or-
s does not contain a date and time that were recognized as the pattern specified in format
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.]
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
System.DateTime Structure, System Namespace
public static DateTime ParseExact(string s, string format, IFormatProvider provider);
Converts the specified String representation of a date and time to its DateTime equivalent using a specified format and IFormatProvider .
- 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 .
A DateTime equivalent to the date and time contained in s.
Exception Type Condition ArgumentNullException s or format is a null reference. FormatException s or format is an empty string. -or-
s does not contain a date and time that were recognized as the pattern specified in format .
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.]
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
System.DateTime Structure, System Namespace
public DateTime Subtract(TimeSpan value);
Subtracts a specified TimeSpan from the current instance.
- value
- An instance of TimeSpan.
A new DateTime instance equal to the date and time represented by the current instance minus the time interval of the specified TimeSpan.
Exception Type Condition ArgumentOutOfRangeException The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
System.DateTime Structure, System Namespace
public TimeSpan Subtract(DateTime value);
Subtracts a specified date and time from the current instance.
- value
- An instance of DateTime.
A TimeSpan interval equal to the date and time represented by the current instance minus the date and time represented by the specified DateTime.
System.DateTime Structure, System Namespace
public DateTime ToLocalTime();
Converts the universal time coordinate (UTC) time value in the current instance to local time.
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 .
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.
System.DateTime Structure, System Namespace
public string ToLongDateString();
Converts the date denoted by the current instance to its equivalent long date String representation.
A String containing the same value as a String returned by System.DateTime.ToString ("D",null
).
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 .
]
System.DateTime Structure, System Namespace
public string ToLongTimeString();
Converts the time denoted by the current instance to its equivalent long time String representation.
A String containing the same value as a String returned by System.DateTime.ToString ("T",null
).
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 .
]
System.DateTime Structure, System Namespace
public string ToShortDateString();
Converts the date denoted by the current instance to its equivalent short date String representation.
A String containing the same value as a String returned by System.DateTime.ToString ("d",null
).
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 .
]
System.DateTime Structure, System Namespace
public string ToShortTimeString();
Converts the time denoted by this instance to its equivalent short time String representation.
A String containing the same value as a String returned by System.DateTime.ToString ("t",null
).
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 .
]
System.DateTime Structure, System Namespace
public string ToString(string format);
Returns a String representation of the value of the current instance.
- format
- A String that specifies the format of the returned string. [Note: For a list of valid values, see DateTimeFormatInfo .]
A String representation of the current instance formatted as specified by format. The string takes into account the current system culture.
Exception Type Condition FormatException The 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.
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.
]
System.DateTime Structure, System Namespace
public override string ToString();
Returns a String representation of the value of the current instance.
A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the current system culture.
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.
]
System.DateTime Structure, System Namespace
public string ToString(string format, IFormatProvider provider);
Returns a String representation of the value of the current instance.
- 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.
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.
Exception Type Condition FormatException The 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.
If provider isnull
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.
]
System.DateTime Structure, System Namespace
public string ToString(IFormatProvider provider);
Returns a String representation of the value of the current instance.
- provider
- A IFormatProvider that supplies a DateTimeFormatInfo containing culture-specific formatting information.
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.
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.
]
System.DateTime Structure, System Namespace
public DateTime ToUniversalTime();
Converts the current DateTime value to coordinated universal time (UTC).
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 .
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.
System.DateTime Structure, System Namespace
public static DateTime operator +(DateTime d, TimeSpan t);
Adds a specified TimeSpan value to a specified DateTime value.
- d
- A DateTime value.
- t
- A TimeSpan value.
A DateTime instance that is the sum of the values of d and t .
Exception Type Condition ArgumentOutOfRangeException The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
The returned value is equivalent toDateTime
( d.Ticks + t.Ticks ).
System.DateTime Structure, System Namespace
public static bool operator ==(DateTime d1, DateTime d2);
Returns a Boolean value indicating whether the two specified instances of DateTime are equal.
- d1
- The first DateTime to compare.
- d2
- The second DateTime to compare.
true
if d1.Ticks value is equal to the d2.Ticks value; otherwise,false
.
System.DateTime Structure, System Namespace
public static bool operator >(DateTime t1, DateTime t2);
Returns a Boolean value indicating whether one specified DateTime is greater than another specified DateTime.
- t1
- A DateTime.
- t2
- A DateTime.
true
if t1.Ticks value is greater than the t2.Ticks value; otherwise,false
.
System.DateTime Structure, System Namespace
public static bool operator >=(DateTime t1, DateTime t2);
Returns a Boolean value indicating whether one specified DateTime is greater than or equal to another specified DateTime.
- t1
- A DateTime.
- t2
- A DateTime.
true
if t1.Ticks value is greater than or equal to t2.Ticks value; otherwise,false
.
System.DateTime Structure, System Namespace
public static bool operator !=(DateTime d1, DateTime d2);
Returns a Boolean value indicating whether two specified instances of DateTime are not equal.
- d1
- A DateTime.
- d2
- A DateTime.
true
if d1.Ticks value is not equal to d2.Ticks value; otherwise,false
.
System.DateTime Structure, System Namespace
public static bool operator <(DateTime t1, DateTime t2);
Returns a Boolean value indicating whether one specified DateTime is less than another specified DateTime.
- t1
- A DateTime.
- t2
- A DateTime.
true
if t1.Ticks value is less than t2.Ticks value; otherwise,false
.
System.DateTime Structure, System Namespace
public static bool operator <=(DateTime t1, DateTime t2);
Returns a Boolean value indicating whether one specified DateTime is less than or equal to another specified DateTime.
- t1
- A DateTime.
- t2
- A DateTime.
true
if t1.Ticks value is less than or equal to t2.Ticks value; otherwise,false
.
System.DateTime Structure, System Namespace
public static DateTime operator -(DateTime d, TimeSpan t);
Subtracts a specified TimeSpan from a specified DateTime .
- d
- A DateTime.
- t
- A TimeSpan.
A DateTime whose value is the value of d minus the value of t .
Exception Type Condition ArgumentOutOfRangeException The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
The returned value is equivalent to DateTime( d.Ticks - t.Ticks ).
System.DateTime Structure, System Namespace
public static TimeSpan operator -(DateTime d1, DateTime d2);
Subtracts a specified DateTime from another specified DateTime value, producing a time interval.
- d1
- A DateTime (the minuend).
- d2
- A DateTime (the subtrahend).
A TimeSpan that is the time interval between d1 and d2.
Exception Type Condition ArgumentOutOfRangeException The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue.
The returned value is equivalent to TimeSpan( d1.Ticks - d2.Ticks).
System.DateTime Structure, System Namespace
public static readonly DateTime MaxValue;
A constant representing the largest possible value of DateTime.
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.
System.DateTime Structure, System Namespace
public static readonly DateTime MinValue;
A constant representing the smallest possible value of DateTime.
This field is read-only.The value of this field is equivalent to 00:00:00.0000000, 1/1/0001.
System.DateTime Structure, System Namespace
public DateTime Date { get; }
Gets the date component of the current instance.
A new DateTime instance with the same date as the current instance, and the time value set to midnight (00:00:00).
This property is read-only.
System.DateTime Structure, System Namespace
public int Day { get; }
Gets the day of the month represented by the current instance.
A Int32 between 1 and 31 set to the day of the month component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public int DayOfYear { get; }
Gets the day of the year represented by the current instance.
A Int32 between 1 and 366 set to the day of the year component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public int Hour { get; }
Gets the hour represented by the current instance.
A Int32 between 0 and 23 set to the hour component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public int Millisecond { get; }
Gets the milliseconds component of the date represented by the current instance.
A Int32 between 0 and 999 set to the milliseconds component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public int Minute { get; }
Gets the minute component of the date represented by the current instance.
A Int32 between 0 and 59 set to the minute component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public int Month { get; }
Gets the month component of the date represented by the current instance.
A Int32 between 1 and 12 set to the month component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public static DateTime Now { get; }
Gets a DateTime representing the current local date and time.
The resolution of this property depends on the system timer.This property is read-only.
System.DateTime Structure, System Namespace
public int Second { get; }
Gets the seconds component of the date represented by the current instance.
A Int32 between 0 and 59 set to the seconds component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public long Ticks { get; }
Gets the number of 100-nanosecond ticks that represent the date and time of the current instance.
A Int64 set to the number of ticks that represent the date and time of the current instance.
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.
System.DateTime Structure, System Namespace
public TimeSpan TimeOfDay { get; }
Gets the time of day of the current instance.
A TimeSpan instance set to the time component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace
public static DateTime Today { get; }
Gets the current date.
A DateTime instance set to the date of the current instance, with the time set to 00:00:00.
This property is read-only.
System.DateTime Structure, System Namespace
public static DateTime UtcNow { get; }
Gets the current time converted to coordinated universal time (UTC).
A DateTime instance set to the current date and time in coordinated universal time (UTC).
This property is read-only.
System.DateTime Structure, System Namespace
public int Year { get; }
Gets the year component of the date represented by the current instance.
A Int32 between 1 and 9999 set to the year component of the current instance.
This property is read-only.
System.DateTime Structure, System Namespace