Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
struct System.​DateTime
Assembly: System.Runtime
Represents an instant in time, typically expressed as a date and time of day.
Fields and Constants
public static readonly
DateTime
MaxValue
Represents the largest possible value of <see cref="T:System.DateTime" /> . This field is read-only.
public static readonly
DateTime
MinValue
Represents the smallest possible value of <see cref="T:System.DateTime" /> . This field is read-only.
public static readonly
DateTime
UnixEpoch
The value of this constant is equivalent to 00:00:00.0000000 UTC, January 1, 1970, in the Gregorian calendar. <see cref="F:System.DateTime.UnixEpoch" /> defines the point in time when Unix time is equal to 0.
Properties
public
DateTime
Date
Gets the date component of this instance.
public
int
Day
Gets the day of the month represented by this instance.
public
DayOfWeek
DayOfWeek
Gets the day of the week represented by this instance.
public
int
DayOfYear
Gets the day of the year represented by this instance.
public
int
Hour
Gets the hour component of the date represented by this instance.
public
DateTimeKind
Kind
Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
public
int
Microsecond
The microseconds component, expressed as a value between 0 and 999.
public
int
Millisecond
Gets the milliseconds component of the date represented by this instance.
public
int
Minute
Gets the minute component of the date represented by this instance.
public
int
Month
Gets the month component of the date represented by this instance.
public
int
Nanosecond
The nanoseconds component, expressed as a value between 0 and 900 (in increments of 100 nanoseconds).
public
int
Second
Gets the seconds component of the date represented by this instance.
public
long
Ticks
Gets the number of ticks that represent the date and time of this instance.
public
TimeSpan
TimeOfDay
Gets the time of day for this instance.
public
int
Year
Gets the year component of the date represented by this instance.
Methods
public
DateTime
Add​(TimeSpan value)
value
A positive or negative time interval.
Returns An object whose value is the sum of the date and time represented by this instance and the time interval represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the value of the specified <see cref="T:System.TimeSpan" /> to the value of this instance.
public
DateTime
AddDays​(double value)
value
A number of whole and fractional days. The <paramref name="value" /> parameter can be negative or positive.
Returns An object whose value is the sum of the date and time represented by this instance and the number of days represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of days to the value of this instance.
public
DateTime
AddHours​(double value)
value
A number of whole and fractional hours. The <paramref name="value" /> parameter can be negative or positive.
Returns An object whose value is the sum of the date and time represented by this instance and the number of hours represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of hours to the value of this instance.
public
DateTime
AddMicroseconds​(double value)
value
A number of whole and fractional microseconds.
The <paramref name="value" /> parameter can be negative or positive.
Note that this value is rounded to the nearest integer.
Returns An object whose value is the sum of the date and time represented by this instance and the number of microseconds represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of microseconds to the value of this instance.
public
DateTime
AddMilliseconds​(double value)
value
A number of whole and fractional milliseconds. The <paramref name="value" /> parameter can be negative or positive. Note that this value is rounded to the nearest integer.
Returns An object whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of milliseconds to the value of this instance.
public
DateTime
AddMinutes​(double value)
value
A number of whole and fractional minutes. The <paramref name="value" /> parameter can be negative or positive.
Returns An object whose value is the sum of the date and time represented by this instance and the number of minutes represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of minutes to the value of this instance.
public
DateTime
AddMonths​(int months)
months
A number of months. The <paramref name="months" /> parameter can be negative or positive.
Returns An object whose value is the sum of the date and time represented by this instance and <paramref name="months" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of months to the value of this instance.
public
DateTime
AddSeconds​(double value)
value
A number of whole and fractional seconds. The <paramref name="value" /> parameter can be negative or positive.
Returns An object whose value is the sum of the date and time represented by this instance and the number of seconds represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of seconds to the value of this instance.
public
DateTime
AddTicks​(long value)
value
A number of 100-nanosecond ticks. The <paramref name="value" /> parameter can be positive or negative.
Returns An object whose value is the sum of the date and time represented by this instance and the time represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of ticks to the value of this instance.
public
DateTime
AddYears​(int value)
value
A number of years. The <paramref name="value" /> parameter can be negative or positive.
Returns An object whose value is the sum of the date and time represented by this instance and the number of years represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that adds the specified number of years to the value of this instance.
public
int
CompareTo​(DateTime value)
value
The object to compare to the current instance.
Returns A signed number indicating the relative values of this instance and the <paramref name="value" /> parameter.
<list type="table">
<listheader>
<term> Value</term>
<description> Description</description>
</listheader>
<item>
<term> Less than zero</term>
<description> This instance is earlier than <paramref name="value" />.</description>
</item>
<item>
<term> Zero</term>
<description> This instance is the same as <paramref name="value" />.</description>
</item>
<item>
<term> Greater than zero</term>
<description> This instance is later than <paramref name="value" />.</description>
</item>
</list>
Compares the value of this instance to a specified <see cref="T:System.DateTime" /> value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified <see cref="T:System.DateTime" /> value.
public
int
CompareTo​(object value)
value
A boxed object to compare, or <see langword="null" /> .
Returns A signed number indicating the relative values of this instance and <paramref name="value" /> .
<list type="table">
<listheader>
<term> Value</term>
<description> Description</description>
</listheader>
<item>
<term> Less than zero</term>
<description> This instance is earlier than <paramref name="value" />.</description>
</item>
<item>
<term> Zero</term>
<description> This instance is the same as <paramref name="value" />.</description>
</item>
<item>
<term> Greater than zero</term>
<description> This instance is later than <paramref name="value" />, or <paramref name="value" /> is <see langword="null" />.</description>
</item>
</list>
Compares the value of this instance to a specified object that contains a specified <see cref="T:System.DateTime" /> value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified <see cref="T:System.DateTime" /> value.
public
bool
Equals​(DateTime value)
value
The object to compare to this instance.
Returns <see langword="true" /> if the <paramref name="value" /> parameter equals the value of this instance; otherwise, <see langword="false" /> .
Returns a value indicating whether the value of this instance is equal to the value of the specified <see cref="T:System.DateTime" /> instance.
public
bool
Equals​(object value)
value
The object to compare to this instance.
Returns <see langword="true" /> if <paramref name="value" /> is an instance of <see cref="T:System.DateTime" /> and equals the value of this instance; otherwise, <see langword="false" /> .
Returns a value indicating whether this instance is equal to a specified object.
public
string[]
GetDateTimeFormats​() Returns A string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.
Converts the value of this instance to all the string representations supported by the standard date and time format specifiers.
public
string[]
GetDateTimeFormats​(char format)
format
A standard date and time format string.
Returns A string array where each element is the representation of the value of this instance formatted with the <paramref name="format" /> standard date and time format specifier.
Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier.
public
string[]
GetDateTimeFormats​(char format,
IFormatProvider provider)
format
A date and time format string.
provider
An object that supplies culture-specific formatting information about this instance.
Returns A string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.
Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier and culture-specific formatting information.
public
string[]
GetDateTimeFormats​(IFormatProvider provider)
provider
An object that supplies culture-specific formatting information about this instance.
Returns A string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.
Converts the value of this instance to all the string representations supported by the standard date and time format specifiers and the specified culture-specific formatting information.
public
int
GetHashCode​() Returns A 32-bit signed integer hash code.
Returns the hash code for this instance.
public
TypeCode
GetTypeCode​() Returns The enumerated constant, <see cref="F:System.TypeCode.DateTime" /> .
Returns the <see cref="T:System.TypeCode" /> for value type <see cref="T:System.DateTime" /> .
public
bool
IsDaylightSavingTime​() Returns <see langword="true" /> if the value of the <see cref="P:System.DateTime.Kind" /> property is <see cref="F:System.DateTimeKind.Local" /> or <see cref="F:System.DateTimeKind.Unspecified" /> and the value of this instance of <see cref="T:System.DateTime" /> is within the daylight saving time range for the local time zone; <see langword="false" /> if <see cref="P:System.DateTime.Kind" /> is <see cref="F:System.DateTimeKind.Utc" /> .
Indicates whether this instance of <see cref="T:System.DateTime" /> is within the daylight saving time range for the current time zone.
public
TimeSpan
Subtract​(DateTime value)
value
The date and time value to subtract.
Returns A time interval that is equal to the date and time represented by this instance minus the date and time represented by <paramref name="value" /> .
Returns a new <see cref="T:System.TimeSpan" /> that subtracts the specified date and time from the value of this instance.
public
DateTime
Subtract​(TimeSpan value)
value
The time interval to subtract.
Returns An object that is equal to the date and time represented by this instance minus the time interval represented by <paramref name="value" /> .
Returns a new <see cref="T:System.DateTime" /> that subtracts the specified duration from the value of this instance.
public
long
ToBinary​() Returns A 64-bit signed integer that encodes the <see cref="P:System.DateTime.Kind" /> and <see cref="P:System.DateTime.Ticks" /> properties.
Serializes the current <see cref="T:System.DateTime" /> object to a 64-bit binary value that subsequently can be used to recreate the <see cref="T:System.DateTime" /> object.
public
long
ToFileTime​() Returns The value of the current <see cref="T:System.DateTime" /> object expressed as a Windows file time.
Converts the value of the current <see cref="T:System.DateTime" /> object to a Windows file time.
public
long
ToFileTimeUtc​() Returns The value of the current <see cref="T:System.DateTime" /> object expressed as a Windows file time.
Converts the value of the current <see cref="T:System.DateTime" /> object to a Windows file time.
public
DateTime
ToLocalTime​() Returns An object whose <see cref="P:System.DateTime.Kind" /> property is <see cref="F:System.DateTimeKind.Local" /> , and whose value is the local time equivalent to the value of the current <see cref="T:System.DateTime" /> object, or <see cref="F:System.DateTime.MaxValue">DateTime.MaxValue</see> if the converted value is too large to be represented by a <see cref="T:System.DateTime" /> object, or <see cref="F:System.DateTime.MinValue">DateTime.MinValue</see> if the converted value is too small to be represented as a <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to local time.
public
string
ToLongDateString​() Returns A string that contains the long date string representation of the current <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent long date string representation.
public
string
ToLongTimeString​() Returns A string that contains the long time string representation of the current <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent long time string representation.
public
double
ToOADate​() Returns A double-precision floating-point number that contains an OLE Automation date equivalent to the value of this instance.
Converts the value of this instance to the equivalent OLE Automation date.
public
string
ToShortDateString​() Returns A string that contains the short date string representation of the current <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent short date string representation.
public
string
ToShortTimeString​() Returns A string that contains the short time string representation of the current <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent short time string representation.
public
string
ToString​() Returns A string representation of the value of the current <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent string representation using the formatting conventions of the current culture.
public
string
ToString​(IFormatProvider provider)
provider
An object that supplies culture-specific formatting information.
Returns A string representation of value of the current <see cref="T:System.DateTime" /> object as specified by <paramref name="provider" /> .
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent string representation using the specified culture-specific format information.
public
string
ToString​(string format)
format
A standard or custom date and time format string.
Returns A string representation of value of the current <see cref="T:System.DateTime" /> object as specified by <paramref name="format" /> .
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent string representation using the specified format and the formatting conventions of the current culture.
public
string
ToString​(string format,
IFormatProvider provider)
format
A standard or custom date and time format string.
provider
An object that supplies culture-specific formatting information.
Returns A string representation of value of the current <see cref="T:System.DateTime" /> object as specified by <paramref name="format" /> and <paramref name="provider" /> .
Converts the value of the current <see cref="T:System.DateTime" /> object to its equivalent string representation using the specified format and culture-specific format information.
public
DateTime
ToUniversalTime​() Returns An object whose <see cref="P:System.DateTime.Kind" /> property is <see cref="F:System.DateTimeKind.Utc" /> , and whose value is the UTC equivalent to the value of the current <see cref="T:System.DateTime" /> object, or <see cref="F:System.DateTime.MaxValue">DateTime.MaxValue</see> if the converted value is too large to be represented by a <see cref="T:System.DateTime" /> object, or <see cref="F:System.DateTime.MinValue">DateTime.MinValue</see> if the converted value is too small to be represented by a <see cref="T:System.DateTime" /> object.
Converts the value of the current <see cref="T:System.DateTime" /> object to Coordinated Universal Time (UTC).
public
bool
TryFormat​(Span`1 utf8Destination,
Int32& bytesWritten,
ReadOnlySpan`1 format = null,
IFormatProvider provider = null)
public
bool
TryFormat​(Span`1 destination,
Int32& charsWritten,
ReadOnlySpan`1 format = null,
IFormatProvider provider = null)
protected
void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public
Type
GetType​()
Inherited from object
Returns The exact runtime type of the current instance.
Gets the <see cref="T:System.Type" /> of the current instance.
protected
object
MemberwiseClone​()
Inherited from object
Returns A shallow copy of the current <see cref="T:System.Object" /> .
Creates a shallow copy of the current <see cref="T:System.Object" /> .