Table of Contents

Struct DateTime

Namespace
Sdl3Sharp.Timing
Assembly
Sdl3Sharp.dll

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

year int

The year part of the DateTime

month int

The month part of the DateTime (in the range from inclusive 1 to inclusive 12)

day int

The day part of the DateTime (in the range from inclusive 1 to inclusive 28, 29, 30, or 31 depending on the month and the year)

hour int

The hour part of the DateTime (in the range from inclusive 0 to inclusive 23)

minute int

The minute part of the DateTime (in the range from inclusive 0 to inclusive 59)

second int

The second part of the DateTime (in the range from inclusive 0 to inclusive 59)

nanosecond int

The nanosecond part of the DateTime (in the range from inclusive 0 to inclusive 999999999)

utcOffset int

The 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

month is less than 1 or greater than 12

  • or - day is less than 1 or greater than 31
  • 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 - nanosecond is less than 0 or greater than 999999999
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 1 to inclusive 28, 29, 30, or 31 depending 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 0 to inclusive 364 or 365 depending 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, or 31 depending 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

int

The hour part of the DateTime (in the range from inclusive 0 to inclusive 23)

Minute

Gets the minute part of the DateTime

public int Minute { get; }

Property Value

int

The minute part of the DateTime (in the range from inclusive 0 to inclusive 59)

Month

Gets the month part of the DateTime

public int Month { get; }

Property Value

int

The month part of the DateTime (in the range from inclusive 1 to inclusive 12)

Nanosecond

Gets the nanosecond part of the DateTime

public int Nanosecond { get; }

Property Value

int

The nanosecond part of the DateTime (in the range from inclusive 0 to inclusive 999999999)

Second

Gets the second part of the DateTime

public int Second { get; }

Property Value

int

The second part of the DateTime (in the range from inclusive 0 to inclusive 59)

UtcOffset

Gets the number of seconds the DateTime (as a local time) is east of UTC

public int UtcOffset { get; }

Property Value

int

The number of seconds the DateTime (as a local time) is east of UTC

Year

Gets the year part of the DateTime

public int Year { get; }

Property Value

int

The year part of the DateTime

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

year int

The year part of the DateTime

month int

The month part of the DateTime (in the range from inclusive 1 to inclusive 12)

day int

The day part of the DateTime (in the range from inclusive 1 to inclusive 28, 29, 30, or 31 depending on the month and year)

hour int

The hour part of the DateTime (in the range from inclusive 0 to inclusive 23)

minute int

The minute part of the DateTime (in the range from inclusive 0 to inclusive 59)

second int

The second part of the DateTime (in the range from inclusive 0 to inclusive 59)

nanosecond int

The nanosecond part of the DateTime (in the range from inclusive 0 to inclusive 999999999)

dayOfWeek DayOfWeek

The day of the week represented by the DateTime (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday)

utcOffset int

The 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

other DateTime

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and 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

nanosecondsRemainder int

The 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

formatProvider IFormatProvider

The provider to use to format the value.

-or-

A null reference (Nothing in 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

format string

The format to use.

-or-

A null reference (Nothing in 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

format string

The format to use.

-or-

A null reference (Nothing in Visual Basic) to use the default format defined for the type of the IFormattable implementation.

formatProvider IFormatProvider

The provider to use to format the value.

-or-

A null reference (Nothing in 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)

Tries to convert a Time to a DateTime

public static bool TryConvertFromTime(Time time, out DateTime dateTime, bool convertToLocalTime = true)

Parameters

time Time

The Time to be converted

dateTime DateTime

The resulting DateTime

convertToLocalTime bool

A value indicating whether the resulting dateTime should be expressed in local time (if true), or otherwise be in Universal Coordinated Time (UTC) (if false)

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)

Tries to convert the DateTime to a Time

public bool TryConvertToTime(out Time time)

Parameters

time Time

The resulting Time

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

destination Span<char>

The span in which to write this instance's value formatted as a span of characters.

charsWritten int

When this method returns, contains the number of characters that were written in destination.

format ReadOnlySpan<char>

A span containing the characters that represent a standard or custom format string that defines the acceptable format for destination.

provider IFormatProvider

An optional object that supplies culture-specific formatting information for destination.

Returns

bool

true if the formatting was successful; otherwise, false.

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

year int

The year part of the date

month int

The month part of the date

day int

The day part of the date

dayOfWeek DayOfWeek

The 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

year int

The year part of the date

month int

The month part of the date

day int

The day part of the date

dayOfYear int

The resulting day of year for the given date (in the range from inclusive 0 to inclusive 364 or 365 depending on the year)

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

year int

The year part of the date

month int

The month part of the date

daysInMonth int

The resulting days in the month for the given date (28, 29, 30, or 31 depending on the month and the year)

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

dateFormat DateFormat

The current preferred date format

timeFormat TimeFormat

The 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

left DateTime

The value to compare with right.

right DateTime

The value to compare with left.

Returns

bool

true if left is equal to right; otherwise, false.

operator !=(in DateTime, in DateTime)

Compares two values to determine inequality.

public static bool operator !=(in DateTime left, in DateTime right)

Parameters

left DateTime

The value to compare with right.

right DateTime

The value to compare with left.

Returns

bool

true if left is not equal to right; otherwise, false.