Table of Contents

Class DateTimeExtensions

Namespace
Garyon.Extensions
Assembly
Garyon.dll

Provides extension functions for the DateTime struct.

public static class DateTimeExtensions
Inheritance
DateTimeExtensions
Inherited Members

Fields

LeapYearDays

public const int LeapYearDays = 366

Field Value

int

NormalYearDays

public const int NormalYearDays = 365

Field Value

int

Methods

AddWeeks(DateTime, int)

Creates a copy of a given DateTime, where the date is adjusted by the specified number of weeks.

[ExcludeFromCodeCoverage]
public static DateTime AddWeeks(this DateTime dateTime, int weeks)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted date.

weeks int

The number of weeks to advance the DateTime by.

Returns

DateTime

A copy of the original DateTime with the date adjusted by the given weeks.

DaysInMonth(DateTime)

Gets the days the month of the provided DateTime has.

[ExcludeFromCodeCoverage]
public static int DaysInMonth(this DateTime dateTime)

Parameters

dateTime DateTime

The DateTime whose month's day count to get.

Returns

int

The number of days the month of dateTime has.

DaysInYear(bool)

Gets the days a year contains, determined by whether it's a leap year or not.

[ExcludeFromCodeCoverage]
public static int DaysInYear(bool isLeapYear)

Parameters

isLeapYear bool

Determines whether the year is a leap year or not.

Returns

int

The number of days the year has.

DaysInYear(DateTime)

Gets the days the year of the provided DateTime has.

[ExcludeFromCodeCoverage]
public static int DaysInYear(this DateTime dateTime)

Parameters

dateTime DateTime

The DateTime whose year's day count to get.

Returns

int

The number of days the year of dateTime has.

DaysInYear(int)

Gets the days the specified year contains.

[ExcludeFromCodeCoverage]
public static int DaysInYear(int year)

Parameters

year int

The year whose days to determine.

Returns

int

The number of days the given year has.

GetComponent(DateTime, DateTimeComponent)

Gets the specified component of the given DateTime instance.

public static int GetComponent(this DateTime dateTime, DateTimeComponent component)

Parameters

dateTime DateTime

The DateTime instance whose component to get.

component DateTimeComponent

The component of the DateTime instance to get. To get the Ticks component, use GetComponentInt64(DateTime, DateTimeComponent).

Returns

int

The requested component of the DateTime instance, represented by the specified DateTimeComponent.

GetComponentInt64(DateTime, DateTimeComponent)

Gets the specified component of the given DateTime instance.

public static long GetComponentInt64(this DateTime dateTime, DateTimeComponent component)

Parameters

dateTime DateTime

The DateTime instance whose component to get.

component DateTimeComponent

The component of the DateTime instance to get.

Returns

long

The requested component of the DateTime instance, represented by the specified DateTimeComponent.

IsInLeapYear(DateTime)

Determines whether the year of the provided DateTime is a leap year.

[ExcludeFromCodeCoverage]
public static bool IsInLeapYear(this DateTime dateTime)

Parameters

dateTime DateTime

The DateTime whose year to determine if it is a leap one.

Returns

bool

Whether the year of dateTime is a leap year.

RoundToNextDay(DateTime, bool)

Rounds the given DateTime to the next day, and possibly retains it if rounded.

public static DateTime RoundToNextDay(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next day.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a day.

Returns

DateTime

The resulting DateTime rounded to the next day, as specified by retainIfRounded.

RoundToNextHour(DateTime, bool)

Rounds the given DateTime to the next hour, and possibly retains it if rounded.

public static DateTime RoundToNextHour(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next hour.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a hour.

Returns

DateTime

The resulting DateTime rounded to the next hour, as specified by retainIfRounded.

RoundToNextMillisecond(DateTime, bool)

Rounds the given DateTime to the next millisecond, and possibly retains it if rounded.

public static DateTime RoundToNextMillisecond(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next millisecond.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a millisecond.

Returns

DateTime

The resulting DateTime rounded to the next millisecond, as specified by retainIfRounded.

RoundToNextMinute(DateTime, bool)

Rounds the given DateTime to the next minute, and possibly retains it if rounded.

public static DateTime RoundToNextMinute(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next minute.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a minute.

Returns

DateTime

The resulting DateTime rounded to the next minute, as specified by retainIfRounded.

RoundToNextMonth(DateTime, bool)

Rounds the given DateTime to the next month, and possibly retains it if rounded.

public static DateTime RoundToNextMonth(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next month.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a month.

Returns

DateTime

The resulting DateTime rounded to the next month, as specified by retainIfRounded.

RoundToNextSecond(DateTime, bool)

Rounds the given DateTime to the next second, and possibly retains it if rounded.

public static DateTime RoundToNextSecond(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next second.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a second.

Returns

DateTime

The resulting DateTime rounded to the next second, as specified by retainIfRounded.

RoundToNextYear(DateTime, bool)

Rounds the given DateTime to the next year, and possibly retains it if rounded.

public static DateTime RoundToNextYear(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the next year.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a year.

Returns

DateTime

The resulting DateTime rounded to the next year, as specified by retainIfRounded.

RoundToPreviousDay(DateTime, bool)

Rounds the given DateTime to the previous day, and possibly retains it if rounded.

public static DateTime RoundToPreviousDay(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous day.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a day.

Returns

DateTime

The resulting DateTime rounded to the previous day, as specified by retainIfRounded.

RoundToPreviousHour(DateTime, bool)

Rounds the given DateTime to the previous hour, and possibly retains it if rounded.

public static DateTime RoundToPreviousHour(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous hour.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a hour.

Returns

DateTime

The resulting DateTime rounded to the previous hour, as specified by retainIfRounded.

RoundToPreviousMillisecond(DateTime, bool)

Rounds the given DateTime to the previous millisecond, and possibly retains it if rounded.

public static DateTime RoundToPreviousMillisecond(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous millisecond.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a millisecond.

Returns

DateTime

The resulting DateTime rounded to the previous millisecond, as specified by retainIfRounded.

RoundToPreviousMinute(DateTime, bool)

Rounds the given DateTime to the previous minute, and possibly retains it if rounded.

public static DateTime RoundToPreviousMinute(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous minute.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a minute.

Returns

DateTime

The resulting DateTime rounded to the previous minute, as specified by retainIfRounded.

RoundToPreviousMonth(DateTime, bool)

Rounds the given DateTime to the previous month, and possibly retains it if rounded.

public static DateTime RoundToPreviousMonth(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous month.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a month.

Returns

DateTime

The resulting DateTime rounded to the previous month, as specified by retainIfRounded.

RoundToPreviousSecond(DateTime, bool)

Rounds the given DateTime to the previous second, and possibly retains it if rounded.

public static DateTime RoundToPreviousSecond(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous second.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a second.

Returns

DateTime

The resulting DateTime rounded to the previous second, as specified by retainIfRounded.

RoundToPreviousYear(DateTime, bool)

Rounds the given DateTime to the previous year, and possibly retains it if rounded.

public static DateTime RoundToPreviousYear(this DateTime dateTime, bool retainIfRounded = false)

Parameters

dateTime DateTime

The date time to round to the previous year.

retainIfRounded bool

Determines whether the result will be the same as the provided DateTime in the case that it already is rounded to a year.

Returns

DateTime

The resulting DateTime rounded to the previous year, as specified by retainIfRounded.

TimeOnly(DateTime)

Creates a DateTime instance only containing the time components of the original instance.

public static DateTime TimeOnly(this DateTime dateTime)

Parameters

dateTime DateTime

The DateTime instance whose time components to retrieve.

Returns

DateTime

A copy of the original DateTime with the year, month and day components set to their default values (01/01/0001), and the time components remaining as is.

Remarks

There is not much usage considered for this method, and the ideal design choice would involve using the TimeOfDay property.

WithComponent(DateTime, int, DateTimeComponent)

Creates a copy of a given DateTime, where the specified component of the copied value is set to the specified value.

[ExcludeFromCodeCoverage]
public static DateTime WithComponent(this DateTime dateTime, int value, DateTimeComponent component)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted time components.

value int

The value to set the component to.

component DateTimeComponent

The component of the DateTime instance that will be changed. Ticks is not accepted in this overload, consider using WithComponent(DateTime, long, DateTimeComponent).

Returns

DateTime

A copy of the original DateTime with the specified component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for value falls out of the valid range for the given component, or an invalid day is represented.

WithComponent(DateTime, long, DateTimeComponent)

Creates a copy of a given DateTime, where the specified component of the copied value is set to the specified value.

[ExcludeFromCodeCoverage]
public static DateTime WithComponent(this DateTime dateTime, long value, DateTimeComponent component)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted time components.

value long

The value to set the component to.

component DateTimeComponent

The component of the DateTime instance that will be changed. Ticks is also accepted.

Returns

DateTime

A copy of the original DateTime with the specified component set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for value falls out of the valid range for the given component, or an invalid day is represented.

WithDay(DateTime, int)

Creates a copy of a given DateTime, where the day of the copied value is set to a specified value.

public static DateTime WithDay(this DateTime dateTime, int day)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted day component.

day int

The value of the day component of the resulting copied DateTime. It must be within the range [1, 31] and available for the given year/month combination.

Returns

DateTime

A copy of the original DateTime with the day set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for day is outside the range [1, 31] -or- the year/month combination originating from the given DateTime does not have the specified day.

WithDayOfWeek(DateTime, DayOfWeek, DayOfWeek)

Creates a copy of a given DateTime, where the day is adjusted to the one specified in the same week.

public static DateTime WithDayOfWeek(this DateTime dateTime, DayOfWeek dayOfWeek, DayOfWeek firstDayOfWeek = DayOfWeek.Sunday)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted day.

dayOfWeek DayOfWeek

The DayOfWeek reflecting the day in the current week the new day will be.

firstDayOfWeek DayOfWeek

The first day of the week, within which the new day will be.

Returns

DateTime

A copy of the original DateTime with the day of week set to the specified value.

WithDayOfYear(DateTime, int)

Creates a copy of a given DateTime, where the day is adjusted to the one specified in the same year.

public static DateTime WithDayOfYear(this DateTime dateTime, int day)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted day.

day int

The day in the same year that will be set. It must range in [1, 365] for normal years, or [1, 366] for leap years.

Returns

DateTime

A copy of the original DateTime with the day of year set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for day is outside the range [1, 365], if the year is a normal one, or [1, 366] if the year is a leap one.

WithHour(DateTime, int)

Creates a copy of a given DateTime, where the hour of the copied value is set to a specified value.

public static DateTime WithHour(this DateTime dateTime, int hour)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted hour component.

hour int

The value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].

Returns

DateTime

A copy of the original DateTime with the hour set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for hour is outside the range [0, 23].

WithHourMinute(DateTime, HourMinute)

Creates a copy of a given DateTime, where the hour and minute components of the copied value are set to specified values.

[ExcludeFromCodeCoverage]
public static DateTime WithHourMinute(this DateTime dateTime, HourMinute hourMinute)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted hour and minute components.

hourMinute HourMinute

The hour and minute components of the resulting copied DateTime.

Returns

DateTime

A copy of the original DateTime with the hour and minute components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for hourMinute represents an hour outside the range [0, 23], or a minute outside the range [0, 59].

WithHourMinute(DateTime, int, int)

Creates a copy of a given DateTime, where the hour and minute components of the copied value are set to specified values.

public static DateTime WithHourMinute(this DateTime dateTime, int hour, int minute)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted hour and minute components.

hour int

The value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].

minute int

The value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].

Returns

DateTime

A copy of the original DateTime with the hour and minute components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for hour is outside the range [0, 23] -or- the given value for minute is outside the range [0, 59].

WithHourMinuteSecond(DateTime, int, int, int)

Creates a copy of a given DateTime, where the hour, minute and second components of the copied value are set to specified values.

public static DateTime WithHourMinuteSecond(this DateTime dateTime, int hour, int minute, int second)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted hour, minute and second components.

hour int

The value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].

minute int

The value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].

second int

The value of the second component of the resulting copied DateTime. It must be within the range [0, 59].

Returns

DateTime

A copy of the original DateTime with the hour, minute and second components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for hour is outside the range [0, 23] -or- the given value for minute is outside the range [0, 59] -or- the given value for second is outside the range [0, 59].

WithHourMinuteSecondMillisecond(DateTime, int, int, int, int)

Creates a copy of a given DateTime, where the hour, minute, second and millisecond components of the copied value are set to specified values.

public static DateTime WithHourMinuteSecondMillisecond(this DateTime dateTime, int hour, int minute, int second, int millisecond)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted hour, minute, second and millisecond components.

hour int

The value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].

minute int

The value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].

second int

The value of the second component of the resulting copied DateTime. It must be within the range [0, 59].

millisecond int

The value of the millisecond component of the resulting copied DateTime. It must be within the range [0, 999].

Returns

DateTime

A copy of the original DateTime with the hour, minute, second and millisecond components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for hour is outside the range [0, 23] -or- the given value for minute is outside the range [0, 59] -or- the given value for second is outside the range [0, 59]. -or- the given value for millisecond is outside the range [0, 999].

WithMillisecond(DateTime, int)

Creates a copy of a given DateTime, where the millisecond of the copied value is set to a specified value.

public static DateTime WithMillisecond(this DateTime dateTime, int millisecond)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted millisecond component.

millisecond int

The value of the millisecond component of the resulting copied DateTime. It must be within the range [0, 999].

Returns

DateTime

A copy of the original DateTime with the millisecond set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for millisecond is outside the range [0, 999].

WithMinute(DateTime, int)

Creates a copy of a given DateTime, where the minute of the copied value is set to a specified value.

public static DateTime WithMinute(this DateTime dateTime, int minute)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted minute component.

minute int

The value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].

Returns

DateTime

A copy of the original DateTime with the minute set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for minute is outside the range [0, 59].

WithMinuteSecond(DateTime, MinuteSecond)

Creates a copy of a given DateTime, where the minute and the second components of the copied value are set to specified values.

[ExcludeFromCodeCoverage]
public static DateTime WithMinuteSecond(this DateTime dateTime, MinuteSecond minuteSecond)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted minute and second components.

minuteSecond MinuteSecond

The minute and second components of the resulting copied DateTime.

Returns

DateTime

A copy of the original DateTime with the minute and second components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for minuteSecond represents a minute or a second outside the range [0, 59].

WithMinuteSecond(DateTime, int, int)

Creates a copy of a given DateTime, where the minute and the second components of the copied value are set to specified values.

public static DateTime WithMinuteSecond(this DateTime dateTime, int minute, int second)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted minute and second components.

minute int

The value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].

second int

The value of the second component of the resulting copied DateTime. It must be within the range [0, 59].

Returns

DateTime

A copy of the original DateTime with the minute and second components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for minute is outside the range [0, 59] -or- the given value for second is outside the range [0, 59].

WithMonth(DateTime, int)

Creates a copy of a given DateTime, where the month of the copied value is set to a specified value.

public static DateTime WithMonth(this DateTime dateTime, int month)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted month component.

month int

The value of the month component of the resulting copied DateTime. It must be within the range [1, 12] and the given day should be available for the resulting year/month combination.

Returns

DateTime

A copy of the original DateTime with the month set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for month is outside the range [1, 12] -or- the resulting year/month combination does not have the day from the original DateTime.

WithMonthDay(DateTime, int, int)

Creates a copy of a given DateTime, where the month and day components of the copied value are set to specified values.

public static DateTime WithMonthDay(this DateTime dateTime, int month, int day)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted month and day components.

month int

The value of the month component of the resulting copied DateTime. It must be within the range [1, 12].

day int

The value of the day component of the resulting copied DateTime. It must be within the range [1, 31].

Returns

DateTime

A copy of the original DateTime with the month and day components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given year, month, day combination is not a valid one -or- the given value for month is outside the range [1, 12] -or- the given value for day is outside the range [1, 31].

WithSecond(DateTime, int)

Creates a copy of a given DateTime, where the second of the copied value is set to a specified value.

public static DateTime WithSecond(this DateTime dateTime, int second)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted second component.

second int

The value of the second component of the resulting copied DateTime. It must be within the range [0, 59].

Returns

DateTime

A copy of the original DateTime with the second set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for second is outside the range [0, 59].

WithSecondMillisecond(DateTime, int, int)

Creates a copy of a given DateTime, where the second and the millisecond components of the copied value are set to specified values.

public static DateTime WithSecondMillisecond(this DateTime dateTime, int second, int millisecond)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted second and millisecond components.

second int

The value of the second component of the resulting copied DateTime. It must be within the range [0, 59].

millisecond int

The value of the second component of the resulting copied DateTime. It must be within the range [0, 999].

Returns

DateTime

A copy of the original DateTime with the second and millisecond components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given value for second is outside the range [0, 59] -or- the given value for millisecond is outside the range [0, 999].

WithTime(DateTime, TimeSpan)

Creates a copy of a given DateTime, where the time components of the copied value are set to specified values.

public static DateTime WithTime(this DateTime dateTime, TimeSpan time)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted time components.

time TimeSpan

The time components of the resulting copied DateTime, whose Days property should be 0.

Returns

DateTime

A copy of the original DateTime with the time components set to the specified values.

Remarks

Consider normalizing the TimeSpan using the WithinDay(TimeSpan) extension.
Since the TimeSpan also retains ticks below the millisecond interval, its ticks will replace the existing DateTime instance's.

Exceptions

ArgumentOutOfRangeException

The given value for time represents an absolute time interval lasting at least an entire day.

WithYear(DateTime, int)

Creates a copy of a given DateTime, where the year of the copied value is set to a specified value.

public static DateTime WithYear(this DateTime dateTime, int year)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted year component.

year int

The value of the year component of the resulting copied DateTime. It must be within the range [1, 9999] and the given day should be available for the resulting year/month combination.

Returns

DateTime

A copy of the original DateTime with the year set to the specified value.

Exceptions

ArgumentOutOfRangeException

The given value for year is outside the range [1, 9999] -or- the resulting year/month combination does not have the day from the original DateTime.

WithYearMonth(DateTime, int, int)

Creates a copy of a given DateTime, where the year and month components of the copied value are set to specified values.

public static DateTime WithYearMonth(this DateTime dateTime, int year, int month)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted year and month components.

year int

The value of the year component of the resulting copied DateTime. It must be within the range [1, 9999].

month int

The value of the month component of the resulting copied DateTime. It must be within the range [1, 12].

Returns

DateTime

A copy of the original DateTime with the year and month components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given year, month, day combination is not a valid one -or- the given value for year is outside the range [1, 9999] -or- the given value for month is outside the range [1, 12].

WithYearMonthDay(DateTime, int, int, int)

Creates a copy of a given DateTime, where the year, month and day components of the copied value are set to specified values.

public static DateTime WithYearMonthDay(this DateTime dateTime, int year, int month, int day)

Parameters

dateTime DateTime

The DateTime from which to create the copy with the adjusted year, month and day components.

year int

The value of the year component of the resulting copied DateTime. It must be within the range [1, 9999].

month int

The value of the month component of the resulting copied DateTime. It must be within the range [1, 12].

day int

The value of the day component of the resulting copied DateTime. It must be within the range [1, 31].

Returns

DateTime

A copy of the original DateTime with the year, month and day components set to the specified values.

Exceptions

ArgumentOutOfRangeException

The given year, month, day combination is not a valid one -or- the given value for year is outside the range [1, 9999] -or- the given value for month is outside the range [1, 12] -or- the given value for day is outside the range [1, 31].