Table of Contents

Struct DateTime

Namespace
System
Assembly
mscorlib.dll

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

public struct DateTime
Inherited Members

Constructors

DateTime(int, int, int)

Initializes a new instance of the DateTime structure to the specified year, month, and day.

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

Parameters

year int

The year (1601 through 3000).

month int

The month (1 through 12).

day int

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.

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

Parameters

year int

The year (1601 through 3000).

month int

The month (1 through 12).

day int

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

hour int

The hours (0 through 23).

minute int

The minutes (0 through 59).

second int

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.

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

Parameters

year int

The year (1601 through 3000).

month int

The month (1 through 12).

day int

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

hour int

The hours (0 through 23).

minute int

The minutes (0 through 59).

second int

The seconds (0 through 59).

millisecond int

The milliseconds (0 through 999).

Exceptions

ArgumentOutOfRangeException

Any parameter out of the accepted ranges

DateTime(long)

Initializes a new instance of the DateTime structure to a specified number of ticks.

public DateTime(long ticks)

Parameters

ticks long

A date and time expressed in the number of 100-nanosecond intervals.

Exceptions

ArgumentOutOfRangeException

ticks - Ticks must be between MinValue and MaxValue.

DateTime(long, DateTimeKind)

Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC).

public DateTime(long ticks, DateTimeKind kind)

Parameters

ticks long

A date and time expressed in the number of 100-nanosecond intervals.

kind DateTimeKind

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.

public static readonly DateTime MaxValue

Field Value

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.

public static readonly DateTime MinValue

Field Value

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.

public static readonly DateTime UnixEpoch

Field Value

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.

public DateTime Date { get; }

Property Value

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.

public int Day { get; }

Property Value

int

The day component, expressed as a value between 1 and 31.

DayOfWeek

Gets the day of the week represented by this instance.

public DayOfWeek DayOfWeek { get; }

Property Value

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.

public int DayOfYear { get; }

Property Value

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.

public int Hour { get; }

Property Value

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.

public DateTimeKind Kind { get; }

Property Value

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.

public int Millisecond { get; }

Property Value

int

The milliseconds component, expressed as a value between 0 and 999.

Minute

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

public int Minute { get; }

Property Value

int

The minute component, expressed as a value between 0 and 59.

Month

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

public int Month { get; }

Property Value

int

The month component, expressed as a value between 1 and 12.

Second

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

public int Second { get; }

Property Value

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.

public long Ticks { get; }

Property Value

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.

public TimeSpan TimeOfDay { get; }

Property Value

TimeSpan

A time interval that represents the fraction of the day that has elapsed since midnight.

Today

Gets the current date.

public static DateTime Today { get; }

Property Value

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).

public static DateTime UtcNow { get; }

Property Value

DateTime

An object whose value is the current UTC date and time.

Year

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

public int Year { get; }

Property Value

int

The year, between 1 and 9999.

Methods

Add(TimeSpan)

Returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.

public DateTime Add(TimeSpan val)

Parameters

val TimeSpan

A positive or negative time interval.

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the time interval represented by val.

AddDays(double)

Returns a new DateTime that adds the specified number of days to the value of this instance.

public DateTime AddDays(double val)

Parameters

val double

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

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the number of days represented by val.

AddHours(double)

Returns a new DateTime that adds the specified number of hours to the value of this instance.

public DateTime AddHours(double val)

Parameters

val double

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

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the number of hours represented by val.

AddMilliseconds(double)

Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.

public DateTime AddMilliseconds(double val)

Parameters

val double

A number of whole and fractional milliseconds. The val parameter can be negative or positive. Note that this value is rounded to the nearest integer.

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by val.

AddMinutes(double)

Returns a new DateTime that adds the specified number of minutes to the value of this instance.

public DateTime AddMinutes(double val)

Parameters

val double

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

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the number of minutes represented by val.

AddSeconds(double)

Returns a new DateTime that adds the specified number of seconds to the value of this instance.

public DateTime AddSeconds(double val)

Parameters

val double

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

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the number of seconds represented by val.

AddTicks(long)

Returns a new DateTime that adds the specified number of ticks to the value of this instance.

public DateTime AddTicks(long val)

Parameters

val long

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

Returns

DateTime

An object whose value is the sum of the date and time represented by this instance and the time represented by val.

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.

public static int Compare(DateTime t1, DateTime t2)

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

int

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

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.

public int CompareTo(object val)

Parameters

val object

A boxed object to compare, or null reference (Nothing in Visual Basic).

Returns

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.

public static int DaysInMonth(int year, int month)

Parameters

year int

The year.

month int

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

Returns

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.

public static bool Equals(DateTime t1, DateTime t2)

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

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.

public override bool Equals(object obj)

Parameters

obj object

The object to compare to this instance.

Returns

bool

true if obj is an instance of DateTime and equals the value of this instance; otherwise, false.

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.

public static DateTime FromUnixTimeSeconds(long seconds)

Parameters

seconds long

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

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.

public override int GetHashCode()

Returns

int

A 32-bit signed integer hash code.

Parse(string)

Converts the string representation of a date and time to its DateTime equivalent by using the conventions of the current culture.

public static DateTime Parse(string s)

Parameters

s string

A string that contains a date and time to convert. See The string to parse for more information.

Returns

DateTime

An object that is equivalent to the date and time contained in s.

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

ArgumentNullException

s is null.

FormatException

Failed to parse s.

Subtract(DateTime)

Subtracts the specified date and time from this instance.

public TimeSpan Subtract(DateTime val)

Parameters

val DateTime

The date and time value to subtract.

Returns

TimeSpan

A time interval that is equal to the date and time represented by this instance minus the date and time represented by val.

Subtract(TimeSpan)

Subtracts the specified duration from this instance.

public DateTime Subtract(TimeSpan val)

Parameters

val TimeSpan

The time interval to subtract.

Returns

DateTime

An object that is equal to the date and time represented by this instance minus the time interval represented by val.

ToString()

Converts the value of the current DateTime object to its equivalent string representation.

public override string ToString()

Returns

string

A string representation of the value of the current DateTime object.

ToString(string)

Converts the value of the current DateTime object to its equivalent string representation using the specified format.

public string ToString(string format)

Parameters

format string

A standard or custom date and time format string (see Remarks).

Returns

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.

public long ToUnixTimeSeconds()

Returns

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.

public static bool TryParse(string s, out DateTime result)

Parameters

s string

A string containing a date and time to convert.

result DateTime

When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is null, is an Empty, or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.

Returns

bool

true if the s parameter was converted successfully; otherwise, false.

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.

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

Parameters

d DateTime

The date and time value to add.

t TimeSpan

The time interval to add.

Returns

DateTime

An object that is the sum of the values of d and t.

operator ==(DateTime, DateTime)

Determines whether two specified instances of DateTime are equal.

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

Parameters

d1 DateTime

The first object to compare.

d2 DateTime

The second object to compare.

Returns

bool

true if d1 and d2 represent the same date and time; otherwise, false.

operator >(DateTime, DateTime)

Determines whether one specified DateTime is greater than another specified DateTime.

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

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

bool

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

operator >=(DateTime, DateTime)

Determines whether one specified DateTime is greater than or equal to another specified DateTime.

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

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

bool

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

operator !=(DateTime, DateTime)

Determines whether two specified instances of DateTime are not equal.

public static bool operator !=(DateTime t1, DateTime t2)

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

bool

true if t1 and t2 do not represent the same date and time; otherwise, false.

operator <(DateTime, DateTime)

Determines whether one specified DateTime is less than another specified DateTime.

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

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

bool

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

operator <=(DateTime, DateTime)

Determines whether one specified DateTime is less than or equal to another specified DateTime.

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

Parameters

t1 DateTime

The first object to compare.

t2 DateTime

The second object to compare.

Returns

bool

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

operator -(DateTime, DateTime)

Subtracts a specified date and time from another specified date and time and returns a time interval.

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

Parameters

d1 DateTime

The date and time value to subtract from (the minuend).

d2 DateTime

The date and time value to subtract (the subtrahend).

Returns

TimeSpan

The time interval between d1 and d2; that is, d1 minus d2.

operator -(DateTime, TimeSpan)

Subtracts a specified time interval from a specified date and time and returns a new date and time.

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

Parameters

d DateTime

The date and time value to subtract from.

t TimeSpan

The time interval to subtract.

Returns

DateTime

An object whose value is the value of d minus the value of t.