Struct DateTime
Represents a calendar date and time, broken down into its parts
public readonly struct DateTime : IEquatable<DateTime>, ISpanFormattable, IFormattable, IEqualityOperators<DateTime, DateTime, bool>
- Implements
- Inherited Members
Constructors
DateTime(int, int, int, int, int, int, int, int)
Creates a new DateTime
public DateTime(int year = 0, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int nanosecond = 0, int utcOffset = 0)
Parameters
yearintThe year part of the DateTime
monthintThe month part of the DateTime (in the range from inclusive
1to inclusive12)dayintThe day part of the DateTime (in the range from inclusive
1to inclusive28,29,30, or31depending on themonthand theyear)hourintThe hour part of the DateTime (in the range from inclusive
0to inclusive23)minuteintThe minute part of the DateTime (in the range from inclusive
0to inclusive59)secondintThe second part of the DateTime (in the range from inclusive
0to inclusive59)nanosecondintThe nanosecond part of the DateTime (in the range from inclusive
0to inclusive999999999)utcOffsetintThe number of seconds the DateTime (as a local time) is east of UTC
Remarks
In contrast to most of the remaining API which uses the Try-method pattern, this constructor intentionally fails by throwing an exception.
If you want to handle failures wrap the call to this constructor in a try-block,
and check TryGet(out string?) for more information when catching a SdlException.
Exceptions
- ArgumentOutOfRangeException
monthis less than1or greater than12- or -
dayis less than1or greater than31 - or -
houris less than0or greater than23 - or -
minuteis less than0or greater than59 - or -
secondis less than0or greater than59 - or -
nanosecondis less than0or greater than999999999
- or -
- SdlException
Couldn't get the day of the week from SDL (check TryGet(out string?) for more information)
Properties
Day
Gets the day part of the DateTime
public int Day { get; }
Property Value
- int
The day part of the DateTime (in the range from inclusive
1to inclusive28,29,30, or31depending on the Month and Year of the DateTime)
DayOfWeek
Gets the day of the week represented by the DateTime
public DayOfWeek DayOfWeek { get; }
Property Value
- DayOfWeek
The day of the week represented by the DateTime (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday)
DayOfYear
Gets the day of year represented by the DateTime
public int DayOfYear { get; }
Property Value
- int
The day of year represented by the DateTime (in the range from inclusive
0to inclusive364or365depending on the Year of the DateTime)
Remarks
In contrast to most of the remaining API which uses the Try-method pattern, this property intentionally fails by throwing an exception.
If you want to handle failures wrap the call to this property in a try-block,
and check TryGet(out string?) for more information when catching a SdlException.
Exceptions
- SdlException
Couldn't get the day of year from SDL (check TryGet(out string?) for more information)
DaysInMonth
Gets the number of days in the month represented by the DateTime
public int DaysInMonth { get; }
Property Value
- int
The number of days in the month represented by the DateTime (
28,29,30, or31depending on the Month and Year of the DateTime)
Remarks
In contrast to most of the remaining API which uses the Try-method pattern, this property intentionally fails by throwing an exception.
If you want to handle failures wrap the call to this property in a try-block,
and check TryGet(out string?) for more information when catching a SdlException.
Exceptions
- SdlException
Couldn't get the days of the month from SDL (check TryGet(out string?) for more information)
Hour
Gets the hour part of the DateTime
public int Hour { get; }
Property Value
Minute
Gets the minute part of the DateTime
public int Minute { get; }
Property Value
Month
Gets the month part of the DateTime
public int Month { get; }
Property Value
Nanosecond
Gets the nanosecond part of the DateTime
public int Nanosecond { get; }
Property Value
Second
Gets the second part of the DateTime
public int Second { get; }
Property Value
UtcOffset
Gets the number of seconds the DateTime (as a local time) is east of UTC
public int UtcOffset { get; }
Property Value
Year
Gets the year part of the DateTime
public int Year { get; }
Property Value
Methods
Deconstruct(out int, out int, out int, out int, out int, out int, out int, out DayOfWeek, out int)
Deconstructs the DateTime
public void Deconstruct(out int year, out int month, out int day, out int hour, out int minute, out int second, out int nanosecond, out DayOfWeek dayOfWeek, out int utcOffset)
Parameters
yearintThe year part of the DateTime
monthintThe month part of the DateTime (in the range from inclusive
1to inclusive12)dayintThe day part of the DateTime (in the range from inclusive
1to inclusive28,29,30, or31depending on themonthandyear)hourintThe hour part of the DateTime (in the range from inclusive
0to inclusive23)minuteintThe minute part of the DateTime (in the range from inclusive
0to inclusive59)secondintThe second part of the DateTime (in the range from inclusive
0to inclusive59)nanosecondintThe nanosecond part of the DateTime (in the range from inclusive
0to inclusive999999999)dayOfWeekDayOfWeekThe day of the week represented by the DateTime (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday)
utcOffsetintThe number of seconds the DateTime (as a local time) is east of UTC
Equals(in DateTime)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(in DateTime other)
Parameters
otherDateTimeAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
ToDateTimeOffset(out int)
Converts to DateTime to a DateTimeOffset
public DateTimeOffset ToDateTimeOffset(out int nanosecondsRemainder)
Parameters
nanosecondsRemainderintThe remaining nanoseconds part of the DateTime which could not be expressed in the resulting DateTimeOffset
Returns
- DateTimeOffset
A DateTimeOffset representing a similiar date and time as the DateTime
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
ToString(IFormatProvider?)
Formats the value of the current instance using the specified format.
public string ToString(IFormatProvider? formatProvider)
Parameters
formatProviderIFormatProviderThe provider to use to format the value.
-or-
A null reference (
Nothingin Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.
Returns
- string
The value of the current instance in the specified format.
ToString(string?)
Formats the value of the current instance using the specified format.
public string ToString(string? format)
Parameters
formatstringThe format to use.
-or-
A null reference (
Nothingin Visual Basic) to use the default format defined for the type of the IFormattable implementation.
Returns
- string
The value of the current instance in the specified format.
ToString(string?, IFormatProvider?)
Formats the value of the current instance using the specified format.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringThe format to use.
-or-
A null reference (
Nothingin Visual Basic) to use the default format defined for the type of the IFormattable implementation.formatProviderIFormatProviderThe provider to use to format the value.
-or-
A null reference (
Nothingin Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.
Returns
- string
The value of the current instance in the specified format.
TryConvertFromTime(Time, out DateTime, bool)
public static bool TryConvertFromTime(Time time, out DateTime dateTime, bool convertToLocalTime = true)
Parameters
timeTimeThe Time to be converted
dateTimeDateTimeThe resulting DateTime
convertToLocalTimeboolA value indicating whether the resulting
dateTimeshould be expressed in local time (iftrue), or otherwise be in Universal Coordinated Time (UTC) (iffalse)
Returns
- bool
true, if the given Time could be successfully converted to a DateTime; otherwise,false(check TryGet(out string?) for more information)
TryConvertToTime(out Time)
public bool TryConvertToTime(out Time time)
Parameters
Returns
- bool
true, if the DateTime could be successfully converted to a Time; otherwise,false(check TryGet(out string?) for more information)
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Tries to format the value of the current instance into the provided span of characters.
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
Parameters
destinationSpan<char>The span in which to write this instance's value formatted as a span of characters.
charsWrittenintWhen this method returns, contains the number of characters that were written in
destination.formatReadOnlySpan<char>A span containing the characters that represent a standard or custom format string that defines the acceptable format for
destination.providerIFormatProviderAn optional object that supplies culture-specific formatting information for
destination.
Returns
TryGetDayOfWeek(int, int, int, out DayOfWeek)
Tries to get the day of the week for a specific year, a specific month, and a specific day
public static bool TryGetDayOfWeek(int year, int month, int day, out DayOfWeek dayOfWeek)
Parameters
yearintThe year part of the date
monthintThe month part of the date
dayintThe day part of the date
dayOfWeekDayOfWeekThe resulting day of the week for the given date (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday)
Returns
- bool
true, if the given date is valid and the resulting day of week was successfully determined; otherwise,false(check TryGet(out string?) for more information)
TryGetDayOfYear(int, int, int, out int)
Tries to get the day of year for a specific year, a specific month, and a specific day
public static bool TryGetDayOfYear(int year, int month, int day, out int dayOfYear)
Parameters
yearintThe year part of the date
monthintThe month part of the date
dayintThe day part of the date
dayOfYearintThe resulting day of year for the given date (in the range from inclusive
0to inclusive364or365depending on theyear)
Returns
- bool
true, if the given date is valid and the resulting day of year was successfully determined; otherwise,false(check TryGet(out string?) for more information)
TryGetDaysInMonth(int, int, out int)
Tries to get the days in a month for a specific year and a specific month
public static bool TryGetDaysInMonth(int year, int month, out int daysInMonth)
Parameters
yearintThe year part of the date
monthintThe month part of the date
daysInMonthintThe resulting days in the month for the given date (
28,29,30, or31depending on themonthand theyear)
Returns
- bool
true, if the given date is valid and the resulting days in the month were successfully determined; otherwise,false(check TryGet(out string?) for more information)
TryGetLocalePreferences(out DateFormat, out TimeFormat)
Tries to get the current preferred date and formats for the system locale
public static bool TryGetLocalePreferences(out DateFormat dateFormat, out TimeFormat timeFormat)
Parameters
dateFormatDateFormatThe current preferred date format
timeFormatTimeFormatThe current preferred time format
Returns
- bool
true, if the current preferred date and time formats could be successfully retrieved; otherwise,false(check TryGet(out string?) for more information)
Remarks
This might be a "slow" call that has to query the operating system. It's best to ask for this once and save the results. However, the preferred formats can change, usually because the user has changed a system preference outside of your program.
To get the current preferred date format only, use TryGetLocalePreference(out DateFormat) instead.
To get the current preferred time format only, use TryGetLocalePreference(out TimeFormat) instead.
Operators
operator ==(in DateTime, in DateTime)
Compares two values to determine equality.
public static bool operator ==(in DateTime left, in DateTime right)
Parameters
Returns
operator !=(in DateTime, in DateTime)
Compares two values to determine inequality.
public static bool operator !=(in DateTime left, in DateTime right)