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
NormalYearDays
public const int NormalYearDays = 365
Field Value
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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted date.
weeksintThe number of weeks to advance the DateTime by.
Returns
DaysInMonth(DateTime)
Gets the days the month of the provided DateTime has.
[ExcludeFromCodeCoverage]
public static int DaysInMonth(this DateTime dateTime)
Parameters
Returns
- int
The number of days the month of
dateTimehas.
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
isLeapYearboolDetermines 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
Returns
- int
The number of days the year of
dateTimehas.
DaysInYear(int)
Gets the days the specified year contains.
[ExcludeFromCodeCoverage]
public static int DaysInYear(int year)
Parameters
yearintThe 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
dateTimeDateTimeThe DateTime instance whose component to get.
componentDateTimeComponentThe 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
dateTimeDateTimeThe DateTime instance whose component to get.
componentDateTimeComponentThe 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
Returns
- bool
Whether the year of
dateTimeis 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
dateTimeDateTimeThe date time to round to the next day.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a day.
Returns
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
dateTimeDateTimeThe date time to round to the next hour.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a hour.
Returns
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
dateTimeDateTimeThe date time to round to the next millisecond.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a millisecond.
Returns
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
dateTimeDateTimeThe date time to round to the next minute.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a minute.
Returns
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
dateTimeDateTimeThe date time to round to the next month.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a month.
Returns
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
dateTimeDateTimeThe date time to round to the next second.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a second.
Returns
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
dateTimeDateTimeThe date time to round to the next year.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a year.
Returns
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
dateTimeDateTimeThe date time to round to the previous day.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a day.
Returns
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
dateTimeDateTimeThe date time to round to the previous hour.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a hour.
Returns
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
dateTimeDateTimeThe date time to round to the previous millisecond.
retainIfRoundedboolDetermines 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
dateTimeDateTimeThe date time to round to the previous minute.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a minute.
Returns
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
dateTimeDateTimeThe date time to round to the previous month.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a month.
Returns
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
dateTimeDateTimeThe date time to round to the previous second.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a second.
Returns
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
dateTimeDateTimeThe date time to round to the previous year.
retainIfRoundedboolDetermines whether the result will be the same as the provided DateTime in the case that it already is rounded to a year.
Returns
TimeOnly(DateTime)
Creates a DateTime instance only containing the time components of the original instance.
public static DateTime TimeOnly(this DateTime dateTime)
Parameters
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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted time components.
valueintThe value to set the component to.
componentDateTimeComponentThe component of the DateTime instance that will be changed. Ticks is not accepted in this overload, consider using WithComponent(DateTime, long, DateTimeComponent).
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
valuefalls 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted time components.
valuelongThe value to set the component to.
componentDateTimeComponentThe component of the DateTime instance that will be changed. Ticks is also accepted.
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
valuefalls 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted day component.
dayintThe 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
Exceptions
- ArgumentOutOfRangeException
The given value for
dayis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted day.
dayOfWeekDayOfWeekThe DayOfWeek reflecting the day in the current week the new day will be.
firstDayOfWeekDayOfWeekThe first day of the week, within which the new day will be.
Returns
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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted day.
dayintThe 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
Exceptions
- ArgumentOutOfRangeException
The given value for
dayis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted hour component.
hourintThe value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
houris 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted hour and minute components.
hourMinuteHourMinuteThe 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
hourMinuterepresents 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted hour and minute components.
hourintThe value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].
minuteintThe 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
houris outside the range [0, 23] -or- the given value forminuteis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted hour, minute and second components.
hourintThe value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].
minuteintThe value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].
secondintThe 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
houris outside the range [0, 23] -or- the given value forminuteis outside the range [0, 59] -or- the given value forsecondis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted hour, minute, second and millisecond components.
hourintThe value of the hour component of the resulting copied DateTime. It must be within the range [0, 23].
minuteintThe value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].
secondintThe value of the second component of the resulting copied DateTime. It must be within the range [0, 59].
millisecondintThe 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
houris outside the range [0, 23] -or- the given value forminuteis outside the range [0, 59] -or- the given value forsecondis outside the range [0, 59]. -or- the given value formillisecondis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted millisecond component.
millisecondintThe value of the millisecond component of the resulting copied DateTime. It must be within the range [0, 999].
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
millisecondis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted minute component.
minuteintThe value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
minuteis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted minute and second components.
minuteSecondMinuteSecondThe 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
minuteSecondrepresents 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted minute and second components.
minuteintThe value of the minute component of the resulting copied DateTime. It must be within the range [0, 59].
secondintThe 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
minuteis outside the range [0, 59] -or- the given value forsecondis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted month component.
monthintThe 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
Exceptions
- ArgumentOutOfRangeException
The given value for
monthis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted month and day components.
monthintThe value of the month component of the resulting copied DateTime. It must be within the range [1, 12].
dayintThe 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
monthis outside the range [1, 12] -or- the given value fordayis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted second component.
secondintThe value of the second component of the resulting copied DateTime. It must be within the range [0, 59].
Returns
Exceptions
- ArgumentOutOfRangeException
The given value for
secondis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted second and millisecond components.
secondintThe value of the second component of the resulting copied DateTime. It must be within the range [0, 59].
millisecondintThe 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
secondis outside the range [0, 59] -or- the given value formillisecondis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted time components.
timeTimeSpanThe time components of the resulting copied DateTime, whose Days property should be 0.
Returns
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
timerepresents 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted year component.
yearintThe 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
Exceptions
- ArgumentOutOfRangeException
The given value for
yearis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted year and month components.
yearintThe value of the year component of the resulting copied DateTime. It must be within the range [1, 9999].
monthintThe 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
yearis outside the range [1, 9999] -or- the given value formonthis 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
dateTimeDateTimeThe DateTime from which to create the copy with the adjusted year, month and day components.
yearintThe value of the year component of the resulting copied DateTime. It must be within the range [1, 9999].
monthintThe value of the month component of the resulting copied DateTime. It must be within the range [1, 12].
dayintThe 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
yearis outside the range [1, 9999] -or- the given value formonthis outside the range [1, 12] -or- the given value fordayis outside the range [1, 31].