Struct DateTime
Represents an instant in time, typically expressed as a date and time of day.
Inherited Members
Namespace: System
Assembly: mscorlib.dll
Syntax
public struct DateTime
Constructors
DateTime(int, int, int)
Initializes a new instance of the DateTime structure to the specified year, month, and day.
Declaration
public DateTime(int year, int month, int day)
Parameters
Type | Name | Description |
---|---|---|
int | year | The year (1601 through 3000). |
int | month | The month (1 through 12). |
int | day | The day (1 through the number of days in month). |
DateTime(int, int, int, int, int, int)
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.
Declaration
public DateTime(int year, int month, int day, int hour, int minute, int second)
Parameters
Type | Name | Description |
---|---|---|
int | year | The year (1601 through 3000). |
int | month | The month (1 through 12). |
int | day | The day (1 through the number of days in month). |
int | hour | The hours (0 through 23). |
int | minute | The minutes (0 through 59). |
int | second | The seconds (0 through 59). |
DateTime(int, int, int, int, int, int, int)
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.
Declaration
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
Parameters
Type | Name | Description |
---|---|---|
int | year | The year (1601 through 3000). |
int | month | The month (1 through 12). |
int | day | The day (1 through the number of days in month). |
int | hour | The hours (0 through 23). |
int | minute | The minutes (0 through 59). |
int | second | The seconds (0 through 59). |
int | millisecond | The milliseconds (0 through 999). |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Any parameter out of the accepted ranges |
DateTime(long)
Initializes a new instance of the DateTime structure to a specified number of ticks.
Declaration
public DateTime(long ticks)
Parameters
Type | Name | Description |
---|---|---|
long | ticks | A date and time expressed in the number of 100-nanosecond intervals. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
DateTime(long, DateTimeKind)
Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC).
Declaration
public DateTime(long ticks, DateTimeKind kind)
Parameters
Type | Name | Description |
---|---|---|
long | ticks | A date and time expressed in the number of 100-nanosecond intervals. |
DateTimeKind | kind | One of the enumeration values that indicates whether ticks specifies a local time, Coordinated Universal Time (UTC), or neither. |
Remarks
nanoFramework doesn't support local time, only UTC, so it's not possible to specify Local.
Fields
MaxValue
Represents the largest possible value of DateTime. This field is read-only.
Declaration
public static readonly DateTime MaxValue
Field Value
Type | Description |
---|---|
DateTime |
Remarks
The value of this constant is equivalent to 23:59:59.9999999, December 31, 3000. This value is specific to nanoFramework. .NET equivalent is 23:59:59.9999999 UTC, December 31, 9999 in the Gregorian calendar.
MinValue
Represents the smallest possible value of DateTime. This field is read-only.
Declaration
public static readonly DateTime MinValue
Field Value
Type | Description |
---|---|
DateTime |
Remarks
The value of this constant is equivalent to 00:00:00.0000000, January 1, 1601. This value is specific to nanoFramework. .NET equivalent is 00:00:00.0000000 UTC, January 1, 0001, in the Gregorian calendar.
UnixEpoch
Represents the Unix Epoch value. This field is read-only.
Declaration
public static readonly DateTime UnixEpoch
Field Value
Type | Description |
---|---|
DateTime |
Remarks
The value of this constant is equivalent to the DateTime corresponding to 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC). This value is specific to nanoFramework.
Properties
Date
Gets the date component of this instance.
Declaration
public DateTime Date { get; }
Property Value
Type | Description |
---|---|
DateTime | A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00). |
Day
Gets the day of the month represented by this instance.
Declaration
public int Day { get; }
Property Value
Type | Description |
---|---|
int | The day component, expressed as a value between 1 and 31. |
DayOfWeek
Gets the day of the week represented by this instance.
Declaration
public DayOfWeek DayOfWeek { get; }
Property Value
Type | Description |
---|---|
DayOfWeek | An enumerated constant that indicates the day of the week of this DateTime value. |
DayOfYear
Gets the day of the year represented by this instance.
Declaration
public int DayOfYear { get; }
Property Value
Type | Description |
---|---|
int | The day of the year, expressed as a value between 1 and 366. |
Hour
Gets the hour component of the date represented by this instance.
Declaration
public int Hour { get; }
Property Value
Type | Description |
---|---|
int | The hour component, expressed as a value between 0 and 23. |
Kind
Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
Declaration
public DateTimeKind Kind { get; }
Property Value
Type | Description |
---|---|
DateTimeKind | One of the enumeration values that indicates what the current time represents. |
Remarks
Despite the default in the full .NET Framework is Local this won't never happen because nanoFramework only supports UTC time.
Millisecond
Gets the milliseconds component of the date represented by this instance.
Declaration
public int Millisecond { get; }
Property Value
Type | Description |
---|---|
int | The milliseconds component, expressed as a value between 0 and 999. |
Minute
Gets the minute component of the date represented by this instance.
Declaration
public int Minute { get; }
Property Value
Type | Description |
---|---|
int | The minute component, expressed as a value between 0 and 59. |
Month
Gets the month component of the date represented by this instance.
Declaration
public int Month { get; }
Property Value
Type | Description |
---|---|
int | The month component, expressed as a value between 1 and 12. |
Second
Gets the seconds component of the date represented by this instance.
Declaration
public int Second { get; }
Property Value
Type | Description |
---|---|
int | The seconds component, expressed as a value between 0 and 59. |
Ticks
Gets the number of ticks that represent the date and time of this instance.
Declaration
public long Ticks { get; }
Property Value
Type | Description |
---|---|
long | The number of ticks that represent the date and time of this instance. The value is between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks |
TimeOfDay
Gets the time of day for this instance.
Declaration
public TimeSpan TimeOfDay { get; }
Property Value
Type | Description |
---|---|
TimeSpan | A time interval that represents the fraction of the day that has elapsed since midnight. |
Today
Gets the current date.
Declaration
public static DateTime Today { get; }
Property Value
Type | Description |
---|---|
DateTime | An object that is set to today's date, with the time component set to 00:00:00. |
UtcNow
Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
Declaration
public static DateTime UtcNow { get; }
Property Value
Type | Description |
---|---|
DateTime | An object whose value is the current UTC date and time. |
Year
Gets the year component of the date represented by this instance.
Declaration
public int Year { get; }
Property Value
Type | Description |
---|---|
int | The year, between 1 and 9999. |
Methods
Add(TimeSpan)
Declaration
public DateTime Add(TimeSpan val)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | val | A positive or negative time interval. |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the time interval represented by |
AddDays(double)
Returns a new DateTime that adds the specified number of days to the value of this instance.
Declaration
public DateTime AddDays(double val)
Parameters
Type | Name | Description |
---|---|---|
double | val | A number of whole and fractional days. The |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the number of days represented by |
AddHours(double)
Returns a new DateTime that adds the specified number of hours to the value of this instance.
Declaration
public DateTime AddHours(double val)
Parameters
Type | Name | Description |
---|---|---|
double | val | A number of whole and fractional hours. The |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the number of hours represented by |
AddMilliseconds(double)
Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.
Declaration
public DateTime AddMilliseconds(double val)
Parameters
Type | Name | Description |
---|---|---|
double | val | A number of whole and fractional milliseconds. The |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by |
AddMinutes(double)
Returns a new DateTime that adds the specified number of minutes to the value of this instance.
Declaration
public DateTime AddMinutes(double val)
Parameters
Type | Name | Description |
---|---|---|
double | val | A number of whole and fractional minutes. The |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the number of minutes represented by |
AddSeconds(double)
Returns a new DateTime that adds the specified number of seconds to the value of this instance.
Declaration
public DateTime AddSeconds(double val)
Parameters
Type | Name | Description |
---|---|---|
double | val | A number of whole and fractional seconds. The |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the number of seconds represented by |
AddTicks(long)
Returns a new DateTime that adds the specified number of ticks to the value of this instance.
Declaration
public DateTime AddTicks(long val)
Parameters
Type | Name | Description |
---|---|---|
long | val | A number of 100-nanosecond ticks. The |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the sum of the date and time represented by this instance and the time represented by |
Compare(DateTime, DateTime)
Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.
Declaration
public static int Compare(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
int | A signed number indicating the relative values of |
CompareTo(object)
Compares the value of this instance to a specified object that contains a specified DateTime value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.
Declaration
public int CompareTo(object val)
Parameters
Type | Name | Description |
---|---|---|
object | val | A boxed object to compare, or null reference (Nothing in Visual Basic). |
Returns
Type | Description |
---|---|
int | A signed number indicating the relative values of this instance and value. |
DaysInMonth(int, int)
Returns the number of days in the specified month and year.
Declaration
public static int DaysInMonth(int year, int month)
Parameters
Type | Name | Description |
---|---|---|
int | year | The year. |
int | month | The month (a number ranging from 1 to 12). |
Returns
Type | Description |
---|---|
int | The number of days in month for the specified year. For example, if month equals 2 for February, the return value is 28 or 29 depending upon whether year is a leap year. |
Equals(DateTime, DateTime)
Returns a value indicating whether two DateTime instances have the same date and time value.
Declaration
public static bool Equals(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if the two values are equal; otherwise, false. |
Equals(object)
Returns a value indicating whether this instance is equal to a specified object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare to this instance. |
Returns
Type | Description |
---|---|
bool | true if |
Overrides
FromUnixTimeSeconds(long)
Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a DateTime value.
Declaration
public static DateTime FromUnixTimeSeconds(long seconds)
Parameters
Type | Name | Description |
---|---|---|
long | seconds | A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative. |
Returns
Type | Description |
---|---|
DateTime | A date and time value that represents the same moment in time as the Unix time. |
Remarks
This method is exclusive of nanoFramework.
GetHashCode()
Returns the hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A 32-bit signed integer hash code. |
Overrides
Parse(string)
Converts the string representation of a date and time to its DateTime equivalent by using the conventions of the current culture.
Declaration
public static DateTime Parse(string s)
Parameters
Type | Name | Description |
---|---|---|
string | s | A string that contains a date and time to convert. See The string to parse for more information. |
Returns
Type | Description |
---|---|
DateTime | An object that is equivalent to the date and time contained in |
Remarks
.NET nanoFramework doesn't support local times so converted values will always have Kind set to Utc.
This attempts to parse s
by using the formatting conventions of Invariant Culture.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FormatException | Failed to parse |
Subtract(DateTime)
Subtracts the specified date and time from this instance.
Declaration
public TimeSpan Subtract(DateTime val)
Parameters
Type | Name | Description |
---|---|---|
DateTime | val | The date and time value to subtract. |
Returns
Type | Description |
---|---|
TimeSpan | A time interval that is equal to the date and time represented by this instance minus the date and time represented by |
Subtract(TimeSpan)
Subtracts the specified duration from this instance.
Declaration
public DateTime Subtract(TimeSpan val)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | val | The time interval to subtract. |
Returns
Type | Description |
---|---|
DateTime | An object that is equal to the date and time represented by this instance minus the time interval represented by |
ToString()
Converts the value of the current DateTime object to its equivalent string representation.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | A string representation of the value of the current DateTime object. |
Overrides
ToString(string)
Converts the value of the current DateTime object to its equivalent string representation using the specified format.
Declaration
public string ToString(string format)
Parameters
Type | Name | Description |
---|---|---|
string | format | A standard or custom date and time format string (see Remarks). |
Returns
Type | Description |
---|---|
string | A string representation of value of the current DateTime object as specified by format. |
ToUnixTimeSeconds()
Returns the number of seconds that have elapsed since 1970-01-01T00:00:00Z.
Declaration
public long ToUnixTimeSeconds()
Returns
Type | Description |
---|---|
long | The number of seconds that have elapsed since 1970-01-01T00:00:00Z. |
Remarks
Unix time represents the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC). It does not take leap seconds into account.
This method is exclusive of nanoFramework.
TryParse(string, out DateTime)
Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.
Declaration
public static bool TryParse(string s, out DateTime result)
Parameters
Type | Name | Description |
---|---|---|
string | s | A string containing a date and time to convert. |
DateTime | result | When this method returns, contains the DateTime value equivalent to the date and time contained in |
Returns
Type | Description |
---|---|
bool | true if the |
Remarks
The TryParse(string, out DateTime) method is similar to the Parse(string) method, except that the TryParse(string, out DateTime) method does not throw an exception if the conversion fails.
The string s
is parsed using formatting information of the Invariant Culture.
Operators
operator +(DateTime, TimeSpan)
Adds a specified time interval to a specified date and time, yielding a new date and time.
Declaration
public static DateTime operator +(DateTime d, TimeSpan t)
Parameters
Type | Name | Description |
---|---|---|
DateTime | d | The date and time value to add. |
TimeSpan | t | The time interval to add. |
Returns
Type | Description |
---|---|
DateTime | An object that is the sum of the values of |
operator ==(DateTime, DateTime)
Determines whether two specified instances of DateTime are equal.
Declaration
public static bool operator ==(DateTime d1, DateTime d2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | d1 | The first object to compare. |
DateTime | d2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if |
operator >(DateTime, DateTime)
Determines whether one specified DateTime is greater than another specified DateTime.
Declaration
public static bool operator >(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if |
operator >=(DateTime, DateTime)
Determines whether one specified DateTime is greater than or equal to another specified DateTime.
Declaration
public static bool operator >=(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if |
operator !=(DateTime, DateTime)
Determines whether two specified instances of DateTime are not equal.
Declaration
public static bool operator !=(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if |
operator <(DateTime, DateTime)
Declaration
public static bool operator <(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if |
operator <=(DateTime, DateTime)
Determines whether one specified DateTime is less than or equal to another specified DateTime.
Declaration
public static bool operator <=(DateTime t1, DateTime t2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | t1 | The first object to compare. |
DateTime | t2 | The second object to compare. |
Returns
Type | Description |
---|---|
bool | true if |
operator -(DateTime, DateTime)
Subtracts a specified date and time from another specified date and time and returns a time interval.
Declaration
public static TimeSpan operator -(DateTime d1, DateTime d2)
Parameters
Type | Name | Description |
---|---|---|
DateTime | d1 | The date and time value to subtract from (the minuend). |
DateTime | d2 | The date and time value to subtract (the subtrahend). |
Returns
Type | Description |
---|---|
TimeSpan | The time interval between |
operator -(DateTime, TimeSpan)
Subtracts a specified time interval from a specified date and time and returns a new date and time.
Declaration
public static DateTime operator -(DateTime d, TimeSpan t)
Parameters
Type | Name | Description |
---|---|---|
DateTime | d | The date and time value to subtract from. |
TimeSpan | t | The time interval to subtract. |
Returns
Type | Description |
---|---|
DateTime | An object whose value is the value of |