DateOnlyExtensions Class
Definition
Provides calendar-arithmetic operations over DateOnly - age, period anchors, weekday navigation, week and quarter numbering, and culture-aware formatting helpers - that complement the small surface shipped with DateOnly itself.
public static class DateOnlyExtensions
- Inheritance
-
DateOnlyExtensions
- Inherited Members
Remarks
DateOnly intentionally exposes very little behavior beyond a numeric day count, leaving callers to build calendar logic by hand or to fall back to DateTime. This class supplies the period and weekday arithmetic that most scheduling, reporting, and fiscal-calendar code needs, expressed directly on DateOnly so the time component cannot leak in by accident.
The API surface clusters into four groups: relative period anchors (FirstDateOfMonth,
FirstDateOfQuarter, FirstDateOfYear and their LastDateOf… counterparts), weekday navigation (
NextDateOfWeek, PreviousDateOfWeek, NearestDateOfWeek, NthDateOfWeekInMonth), period
predicates and counters (IsLeapYear, IsWeekend, IsInRange, WeekOfMonth,
WeekOfYear, Quarter), and culture-aware formatting (DayName, MonthName).
Methods that emit text or read Calendar data accept an optional culture or calendar argument; when omitted they fall back to CurrentCulture, so the same call produces different text on different threads. Methods that perform pure date arithmetic (period anchors, weekday navigation) are culture-neutral, allocation-free, and deterministic. ArgumentOutOfRangeException is thrown when an arithmetic operation would leave the supported DateOnly range.
When compiled with a tool-chain that supports C# 14 extension members, the parameterless predicate and
scalar-accessor helpers are exposed as extension properties (for example date.IsFirstDateOfMonth); otherwise
they compile as classic extension methods (for example date.IsFirstDateOfMonth()).
var date = new DateOnly(2025, 4, 30);
// Anchor to the first Monday in this month.
DateOnly firstMonday = date.FirstDateOfWeekInMonth(DayOfWeek.Monday); // => 2025-04-07
// Walk to the previous Friday, even if today is already a Friday.
DateOnly priorFriday = date.PreviousDateOfWeek(DayOfWeek.Friday); // => 2025-04-25
// Compute the calendar week within the month using ISO rules.
int weekOfMonth = date.WeekOfMonth(CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); // => 5
Methods
Add(DateOnly, int, int, int)
Returns a new DateOnly obtained by adding the specified number of years, months, and days to the
supplied date.
public static DateOnly Add(this DateOnly date, int years, int months, int days)
Parameters
dateDateOnlyThe date value to which the offsets are applied.
yearsintThe number of calendar years to add. A negative value subtracts years.
monthsintThe number of calendar months to add. A negative value subtracts months.
daysintThe number of days to add. A negative value subtracts days.
Returns
Remarks
Adjustments are applied in the order years, then months, then days. When the resulting day does not exist in the target month (e.g. February 30), the date is clamped to the last valid day of that month, accounting for leap years and varying month lengths.
Examples:
var d1 = new DateOnly(2023, 1, 31);
var result1 = d1.Add(0, 1, 0); // → 2023-02-28 (non-leap year)
var d2 = new DateOnly(2020, 1, 31);
var result2 = d2.Add(0, 1, 0); // → 2020-02-29 (leap year)
var d3 = new DateOnly(2024, 2, 29);
var result3 = d3.Add(1, 0, 0); // → 2025-02-28 (2025 is not a leap year)
Exceptions
- ArgumentOutOfRangeException
Thrown if the resulting date is earlier than MinValue or later than MaxValue.
AddFiscalYears(DateOnly, int, IQuarterDefinitionProvider)
Returns a new DateOnly obtained by advancing or retreating date by the signed
number of fiscal years specified in count.
public static DateOnly AddFiscalYears(this DateOnly date, int count, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe starting date.
countintThe signed number of fiscal years to apply.
providerIQuarterDefinitionProviderThe provider that defines the fiscal year boundaries.
Returns
- DateOnly
A DateOnly whose value is offset by
countfiscal years, preserving the day-index within the fiscal year ofdate. The result is clamped to the last day of the target fiscal year when the source day-index does not fit (e.g. a Q4 53rd-week date mapped onto a 52-week year).
Exceptions
- ArgumentNullException
Thrown when
provideris null.
Age(DateOnly)
Returns the age in full calendar years between the specified date and today's date.
public static int Age(this DateOnly date)
Parameters
dateDateOnlyThe earlier date to calculate from, typically representing a birth date or other reference point.
Returns
- int
The number of full calendar years that have elapsed between
dateand today. Returns0ifdateoccurs after today.
Remarks
This overload determines the number of full years that have passed by comparing the year, month, and day
components. If the month and day of date have not yet occurred in the current year, the
result is decremented by one.
If date is February 29 in a leap year and today is not a leap year, the comparison is
performed as if the date were February 28.
The result is clamped to 0 to avoid returning negative values when date is in the
future.
Age(DateOnly, DateOnly)
Returns the age in full calendar years between the specified date and a supplied reference
date.
public static int Age(this DateOnly date, DateOnly asAtDate)
Parameters
dateDateOnlyThe earlier date to calculate from, typically representing a birth date or other reference point.
asAtDateDateOnlyThe later date to calculate to, representing the point in time at which the age is evaluated.
Returns
- int
The number of full calendar years that have elapsed between
dateandasAtDate. Returns0ifasAtDateoccurs beforedate.
Remarks
This overload determines the number of full years that have passed by comparing the year, month, and day
components. If the month and day of date have not yet occurred in the year of
asAtDate, the result is decremented by one.
If date is February 29 in a leap year and asAtDate is in a non-leap
year, the comparison is performed as if the date were February 28.
The result is clamped to 0 to avoid returning negative values when date is after
asAtDate.
DayName(DateOnly)
Returns the full name of the day of the week for the specified DateOnly, using the formatting rules of CurrentCulture.
public static string DayName(this DateOnly date)
Parameters
Returns
- string
A string containing the localized full day name, formatted using CurrentCulture.
Remarks
This overload uses the GetDayName(DayOfWeek) method of the current culture to retrieve the day name. For culture-specific results, use the DayName(DateOnly, CultureInfo?) overload.
DayName(DateOnly, CultureInfo?)
Returns the full name of the day of the week for the specified DateOnly, using the formatting rules of the supplied or current culture.
public static string DayName(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value whose DayOfWeek is used to determine the name.
cultureCultureInfoAn optional CultureInfo used to format the result. If null, CurrentCulture is used.
Returns
- string
A string containing the localized full day name for
date, formatted using the supplied or current culture.
Remarks
This overload uses the GetDayName(DayOfWeek) method of the supplied or current culture to retrieve the day name.
DaysInMonth(DateOnly)
Returns the number of days in the calendar month of the specified DateOnly, using the proleptic Gregorian calendar.
public static int DaysInMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value whose year and month are used to determine the result.
Returns
- int
The total number of days in the specified month and year of
date, based on the GregorianCalendar.
Remarks
This overload always evaluates the result using the proleptic Gregorian calendar, regardless of the current culture or calendar settings. For culture-specific results, use the DaysInMonth(DateOnly, CultureInfo?) or DaysInMonth(DateOnly, Calendar?) overload.
DaysInMonth(DateOnly, Calendar?)
Returns the number of days in the calendar month of the specified DateOnly, using the supplied or current culture's calendar.
public static int DaysInMonth(this DateOnly date, Calendar? calendar)
Parameters
dateDateOnlyThe date value whose year and month are used to determine the result.
calendarCalendarAn optional Calendar instance used to evaluate the result. If null, the Calendar of CurrentCulture is used.
Returns
- int
The total number of days in the specified month and year of
date, based on the rules of the supplied or current calendar.
Remarks
This overload supports calendar-aware computations for systems such as HebrewCalendar,
HijriCalendar, JapaneseCalendar, and others supported by .NET.
date is first projected into the target calendar, so the result is equivalent to
calendar.GetDaysInMonth(calendar.GetYear(dateTime), calendar.GetMonth(dateTime)) for the
DateTime at midnight of date - the length of the calendar's own month
containing the date, not the Gregorian month. If calendar is null, the
Calendar of CurrentCulture is used.
This method does not account for leap months. For calendars that support leap months or multiple eras, consider
using GetDaysInMonth(year, month, era) instead.
DaysInMonth(DateOnly, CultureInfo?)
Returns the number of days in the calendar month of the specified DateOnly, using the calendar associated with the supplied culture.
public static int DaysInMonth(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value whose year and month are used to determine the result.
cultureCultureInfoAn optional CultureInfo that supplies the calendar. If null, CurrentCulture is used.
Returns
- int
The total number of days in the specified month and year of
date, based on the calendar ofculture.
Remarks
This overload retrieves the Calendar from the culture's
Calendar property and returns the number of days in the month of that
calendar's own year/month reckoning that contains date.
This is useful when working with cultures that use non-Gregorian calendars such as HebrewCalendar or HijriCalendar. If the calendar supports leap months or eras, this method does not account for them explicitly. For precise control, use the overload that accepts a Calendar directly.
DaysInYear(DateOnly)
Returns the number of days in the calendar year of the specified DateOnly, using the calendar of CurrentCulture.
public static int DaysInYear(this DateOnly date)
Parameters
dateDateOnlyThe date value whose year is used to determine the result.
Returns
- int
The total number of days in the year of
date, as defined by the calendar of CurrentCulture.
Remarks
This overload uses the calendar the current culture is set to use: the Calendar of CurrentCulture, which is not necessarily the culture's default Calendar. The result may vary depending on the calendar system (e.g. Gregorian, Hebrew, Hijri).
DaysInYear(DateOnly, Calendar?)
Returns the number of days in the calendar year of the specified DateOnly, using the supplied or current calendar.
public static int DaysInYear(this DateOnly date, Calendar? calendar)
Parameters
dateDateOnlyThe date value whose year is used to determine the result.
calendarCalendarAn optional Calendar used to evaluate the result. If null, the Calendar of CurrentCulture is used.
Returns
- int
The number of days in the year of
date, based on the supplied or fallback calendar.
Remarks
Use this overload when you want to explicitly calculate based on a specific calendar system (e.g.
GregorianCalendar, HebrewCalendar). If calendar is
null, the Calendar of
CurrentCulture is used.
date is first projected into the target calendar, so the result is equivalent to
calendar.GetDaysInYear(calendar.GetYear(dateTime)) for the DateTime at midnight of
date - the length of the calendar's own year containing the date, not the Gregorian year.
FirstDateOfFiscalYear(int, IQuarterDefinitionProvider)
Returns the first calendar day of the supplied fiscal year under the supplied IQuarterDefinitionProvider.
public static DateOnly FirstDateOfFiscalYear(int fiscalYear, IQuarterDefinitionProvider provider)
Parameters
fiscalYearintThe fiscal year whose start date is requested.
providerIQuarterDefinitionProviderThe provider that defines the fiscal year boundaries.
Returns
Exceptions
- ArgumentNullException
Thrown when
provideris null.
FirstDateOfMonth(DateOnly)
Returns a new DateOnly representing the first day of the same calendar month and year as the
specified date.
public static DateOnly FirstDateOfMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value whose year and month are used to determine the result.
Returns
Remarks
This method calculates the first day of the month using Gregorian calendar rules.
Example:
var date = new DateOnly(2025, 7, 15);
var result = date.FirstDateOfMonth(); // → 2025-07-01
FirstDateOfQuarter(DateOnly)
Returns a new DateOnly representing the first day of the calendar quarter that contains the
specified date, using the standard calendar quarter definition.
public static DateOnly FirstDateOfQuarter(this DateOnly date)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
Returns
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember:
- Q1January - March
- Q2April - June
- Q3July - September
- Q4October - December
FirstDateOfQuarter(DateOnly, CalendarQuarterDefinition)
Returns a new DateOnly representing the first day of the quarter that contains the specified
date, using the specified calendar quarter definition.
public static DateOnly FirstDateOfQuarter(this DateOnly date, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
Remarks
The definition controls whether quarters are aligned to the first day of a month (e.g.
January - March) or anchored to a custom day-of-month boundary.
For provider-driven (e.g. 4-4-5 fiscal) quarters, use the FirstDateOfQuarter(DateOnly, IQuarterDefinitionProvider) overload instead.
Exceptions
- ArgumentOutOfRangeException
Thrown if
definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
FirstDateOfQuarter(DateOnly, IQuarterDefinitionProvider)
Returns a new DateOnly representing the first day of the quarter that contains the specified
date, using a custom IQuarterDefinitionProvider.
public static DateOnly FirstDateOfQuarter(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
Remarks
This overload supports advanced or domain-specific quarter systems by delegating boundary logic to the supplied
provider - for example, 4-4-5 retail calendars or regional fiscal quarters.
Exceptions
- ArgumentNullException
Thrown if
provideris null.- ArgumentOutOfRangeException
Thrown if the
providerreturns a date outside the range of MinValue and MaxValue.
FirstDateOfWeek(DateOnly)
Returns a new DateOnly representing the first day of the week that contains the specified
date, using the first day of the week defined by CurrentCulture.
public static DateOnly FirstDateOfWeek(this DateOnly date)
Parameters
dateDateOnlyThe date value used to determine the containing week.
Returns
Remarks
This overload uses CurrentCulture to determine the first day of the week, based on FirstDayOfWeek.
FirstDateOfWeek(DateOnly, WorkingDaysOfWeek)
Returns a new DateOnly representing the first day of the week that contains the specified
date, using a start-of-week inferred from the specified WorkingDaysOfWeek.
public static DateOnly FirstDateOfWeek(this DateOnly date, WorkingDaysOfWeek workingWeek)
Parameters
dateDateOnlyThe date value used to determine the containing week.
workingWeekWorkingDaysOfWeekA WorkingDaysOfWeek used to infer the first day of the week. For example, MondayToFriday implies a Monday start.
Returns
Remarks
The method infers the start of the week based on the specified workingWeek value. If
AllDays is supplied, the method defaults to using
Monday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined WorkingDaysOfWeek value, -or- the resulting date is earlier than MinValue.
FirstDateOfWeek(DateOnly, CultureInfo?)
Returns a new DateOnly representing the first day of the week that contains the specified
date, using the first day of the week defined by the supplied or current culture.
public static DateOnly FirstDateOfWeek(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value used to determine the containing week.
cultureCultureInfoAn optional CultureInfo that defines the first day of the week via FirstDayOfWeek. If null, CurrentCulture is used.
Returns
Remarks
This method computes the day offset between date and the culture-specific first day of the
week, and subtracts that offset.
Exceptions
- ArgumentOutOfRangeException
Thrown if the resulting date is earlier than MinValue.
FirstDateOfWeekInMonth(DateOnly, DayOfWeek)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the same calendar month and year as the specified date.
public static DateOnly FirstDateOfWeekInMonth(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe date value whose month and year are used to determine the result.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the month. For example, Monday returns the first Monday.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the same calendar month and year asdate.
Remarks
The search begins on the first day of the month and proceeds forward to locate the first matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
FirstDateOfWeekInQuarter(DateOnly, DayOfWeek)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the calendar quarter that contains the specified date, using
the standard calendar quarter definition.
public static DateOnly FirstDateOfWeekInQuarter(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the first Monday.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the quarter that containsdate.
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember. The search begins on the first day of the quarter and proceeds forward to locate the first matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
FirstDateOfWeekInQuarter(DateOnly, DayOfWeek, CalendarQuarterDefinition)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the quarter that contains the specified date, using the
supplied calendar quarter definition.
public static DateOnly FirstDateOfWeekInQuarter(this DateOnly date, DayOfWeek dayOfWeek, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the first Monday.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the quarter that containsdate.
Remarks
The start of the quarter is computed using definition, and the search proceeds forward to
the first date that matches the specified dayOfWeek.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration, -or-definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
FirstDateOfWeekInQuarter(DateOnly, DayOfWeek, IQuarterDefinitionProvider)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the quarter that contains the specified date, using a custom
IQuarterDefinitionProvider.
public static DateOnly FirstDateOfWeekInQuarter(this DateOnly date, DayOfWeek dayOfWeek, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the first Monday.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the quarter that containsdate.
Remarks
The start of the quarter is determined by the supplied provider, and the search proceeds
forward to the first date that matches the specified dayOfWeek.
Exceptions
- ArgumentNullException
Thrown if
provideris null.- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
FirstDateOfWeekInYear(DateOnly, DayOfWeek)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the same calendar year as the specified date.
public static DateOnly FirstDateOfWeekInYear(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe date value whose year is used to determine the result.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the year. For example, Monday returns the first Monday.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the same calendar year asdate.
Remarks
The search begins on January 1 of the year and proceeds forward to locate the first matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
FirstDateOfYear(DateOnly)
Returns a new DateOnly representing the first day of the same calendar year as the specified
date.
public static DateOnly FirstDateOfYear(this DateOnly date)
Parameters
dateDateOnlyThe date value whose year is used to determine the result.
Returns
Remarks
This method calculates the first day of the year using Gregorian calendar rules.
Example:
var date = new DateOnly(2025, 7, 15);
var result = date.FirstDateOfYear(); // → 2025-01-01
FiscalYear(DateOnly, IQuarterDefinitionProvider)
Returns the fiscal year that contains the supplied DateOnly under the supplied IQuarterDefinitionProvider.
public static int FiscalYear(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date to identify.
providerIQuarterDefinitionProviderThe provider that defines the fiscal year boundaries.
Returns
- int
The fiscal year number under the provider's conventions.
Exceptions
- ArgumentNullException
Thrown when
provideris null.
FromDayOfYear(int, int)
Returns a new DateOnly representing the dayOfYear-th day of the specified
year.
public static DateOnly FromDayOfYear(int year, int dayOfYear)
Parameters
Returns
Remarks
This method is the inverse of the DayOfYear property. Day numbering is one-based, so day
1 is January 1 and the final day (365 in a common year, 366 in a leap year) is December 31.
The mapping depends on whether year is a leap year: day 60 resolves to February 29 in
a leap year but to March 1 in a common year.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than 1 or greater than 9999, -or-dayOfYearis less than 1 or greater than the number of days inyear(365 in a common year, 366 in a leap year).
FromUnixTimeMilliseconds(long)
Returns a new DateOnly representing the calendar date corresponding to the specified Unix timestamp, expressed in milliseconds since 1970-01-01T00:00:00Z.
public static DateOnly FromUnixTimeMilliseconds(long timestamp)
Parameters
timestamplongThe number of milliseconds that have elapsed since the Unix epoch.
Returns
Remarks
Use ToUnixTimeMilliseconds(DateOnly) to perform the inverse conversion.
Exceptions
- ArgumentOutOfRangeException
Thrown if
timestampis outside the range supported for conversion to DateOnly .
- See Also
FromUnixTimeSeconds(long)
Returns a new DateOnly representing the calendar date corresponding to the specified Unix timestamp, expressed in seconds since 1970-01-01T00:00:00Z.
public static DateOnly FromUnixTimeSeconds(long timestamp)
Parameters
timestamplongThe number of seconds that have elapsed since the Unix epoch.
Returns
Remarks
Use ToUnixTimeSeconds(DateOnly) to perform the inverse conversion.
Exceptions
- ArgumentOutOfRangeException
Thrown if
timestampis outside the range supported for conversion to DateOnly .
- See Also
GetFirstDateOfIsoWeek(int, int)
Returns a new DateOnly representing the first day (Monday) of the specified ISO 8601 week and year.
public static DateOnly GetFirstDateOfIsoWeek(int isoYear, int isoWeek)
Parameters
isoYearintThe ISO 8601 year, defined as the year containing the Thursday of the first ISO week. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.isoWeekintThe ISO 8601 week number to evaluate, ranging from 1 to the number of ISO weeks in the supplied year.
Returns
Remarks
This method computes the first day of a given ISO 8601 week by anchoring on January 4 (which always falls in ISO week 1), then backtracking to the preceding Monday and advancing by the supplied number of weeks.
The ISO 8601 calendar follows these rules:
- weeks begin on Monday;
- week 1 is the first week containing at least four days of the new year;
- years contain either 52 or 53 weeks.
This member delegates to GetFirstDateOfIsoWeek(int, int) - the twins share one implementation, so both surfaces always agree.
Exceptions
- ArgumentOutOfRangeException
Thrown if
isoYearis less than theYearof MinValue or greater than that of MaxValue, -or-isoWeekis less than 1 or greater than the number of ISO weeks inisoYear.
GetFirstDateOfMonth(int, int)
Returns a new DateOnly representing the first day of the specified calendar month and year.
public static DateOnly GetFirstDateOfMonth(int year, int month)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.monthintThe calendar month of the result. Must be between 1 and 12, inclusive, where 1 represents January and 12 represents December.
Returns
Remarks
This method uses Gregorian calendar rules to determine the resulting date.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-monthis less than 1 or greater than 12.
GetFirstDateOfQuarter(int, int)
Returns a new DateOnly representing the first day of the specified calendar
quarter in the given year, using the standard calendar quarter
definition.
public static DateOnly GetFirstDateOfQuarter(int year, int quarter)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.quarterintThe quarter number, from 1 (Jan - Mar) through 4 (Oct - Dec).
Returns
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember.
Exceptions
- ArgumentOutOfRangeException
Thrown if
quarteris less than 1 or greater than 4.
GetFirstDateOfQuarter(int, int, CalendarQuarterDefinition)
Returns a new DateOnly representing the first day of the specified quarter
and year, using the supplied calendar quarter definition.
public static DateOnly GetFirstDateOfQuarter(int year, int quarter, CalendarQuarterDefinition definition)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive. If the quarter begins in the next calendar year (based on the definition's anchor), the year will be incremented accordingly.quarterintThe quarter number, from 1 through 4.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarters are aligned.
Returns
Remarks
The definition controls whether quarters are aligned to the first day of a month or anchored
to a custom day-of-month boundary.
Exceptions
- ArgumentOutOfRangeException
Thrown if
quarteris less than 1 or greater than 4, -or-definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use a provider-based overload instead.
GetFirstDateOfWeekInMonth(int, int, DayOfWeek)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek in the given month and year.
public static DateOnly GetFirstDateOfWeekInMonth(int year, int month, DayOfWeek dayOfWeek)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.monthintThe calendar month of the result, from 1 through 12.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the month. For example, Monday returns the first Monday.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the specified month and year.
Remarks
The search begins on the first day of the month and proceeds forward to locate the first matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-monthis less than 1 or greater than 12, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration.
GetFirstDateOfWeekInQuarter(int, int, DayOfWeek)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the specified calendar quarter and year,
using the standard calendar quarter definition.
public static DateOnly GetFirstDateOfWeekInQuarter(int year, int quarter, DayOfWeek dayOfWeek)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.quarterintThe quarter number, from 1 (Jan - Mar) through 4 (Oct - Dec).
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the first Monday.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the specified quarter and year.
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-quarteris less than 1 or greater than 4, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration.
GetFirstDateOfWeekInQuarter(int, int, DayOfWeek, CalendarQuarterDefinition)
Returns a new DateOnly representing the first occurrence of the specified
DayOfWeek within the specified quarter and year, using
the supplied calendar quarter definition.
public static DateOnly GetFirstDateOfWeekInQuarter(int year, int quarter, DayOfWeek dayOfWeek, CalendarQuarterDefinition definition)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.quarterintThe quarter number, from 1 through 4.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the first Monday.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
- DateOnly
A DateOnly value set to the first occurrence of
dayOfWeekwithin the specified quarter and year.
Remarks
The start of the quarter is computed using definition, and the search proceeds forward to
the first date that matches the specified dayOfWeek.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-quarteris less than 1 or greater than 4, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration, -or-definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
GetIsoWeeksInYear(int)
Returns the number of ISO 8601 weeks in the specified year.
public static int GetIsoWeeksInYear(int year)
Parameters
yearintThe ISO 8601 year to evaluate. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.
Returns
- int
The number of ISO 8601 weeks in the supplied year - either 52 or 53.
Remarks
According to ISO 8601, a year contains 53 weeks if either of the following is true:
- January 1 of the supplied year falls on a Thursday;
- December 31 of the supplied year falls on a Thursday (equivalent to January 1 of the following year falling on a Friday).
All other years contain exactly 52 weeks. This member delegates to GetIsoWeeksInYear(int) - the twins share one implementation, so both surfaces always agree.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue.
GetLastDateOfIsoWeek(int, int)
Returns a new DateOnly representing the last day (Sunday) of the specified ISO 8601 week and year.
public static DateOnly GetLastDateOfIsoWeek(int isoYear, int isoWeek)
Parameters
isoYearintThe ISO 8601 year, defined as the year containing the Thursday of the first ISO week. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.isoWeekintThe ISO 8601 week number to evaluate, ranging from 1 to the number of ISO weeks in the supplied year.
Returns
Remarks
This method computes the last day of a given ISO 8601 week by anchoring on January 4 (which always falls in ISO week 1), backtracking to the preceding Monday, advancing by the supplied number of weeks, and adding six days to reach the Sunday of that week.
The ISO 8601 calendar follows these rules:
- weeks begin on Monday and end on Sunday;
- week 1 is the first week containing at least four days of the new year;
- years contain either 52 or 53 weeks.
For the corresponding start of the week, use GetFirstDateOfIsoWeek(int, int). This member delegates to GetLastDateOfIsoWeek(int, int) - the twins share one implementation, so both surfaces always agree.
Exceptions
- ArgumentOutOfRangeException
Thrown if
isoYearis less than theYearof MinValue or greater than that of MaxValue, -or-isoWeekis less than 1 or greater than the number of ISO weeks inisoYear.
GetLastDateOfMonth(int, int)
Returns a new DateOnly representing the last day of the specified calendar month and year.
public static DateOnly GetLastDateOfMonth(int year, int month)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.monthintThe calendar month of the result. Must be between 1 and 12, inclusive, where 1 represents January and 12 represents December.
Returns
Remarks
This method uses Gregorian calendar rules to determine the number of days in the specified month, including leap-year adjustments for February.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-monthis less than 1 or greater than 12.
GetLastDateOfQuarter(int, int)
Returns a new DateOnly representing the last day of the specified calendar
quarter in the given year, using the standard calendar quarter
definition.
public static DateOnly GetLastDateOfQuarter(int year, int quarter)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.quarterintThe quarter number, from 1 (Jan - Mar) through 4 (Oct - Dec).
Returns
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember.
Exceptions
- ArgumentOutOfRangeException
Thrown if
quarteris less than 1 or greater than 4.
GetLastDateOfQuarter(int, int, CalendarQuarterDefinition)
Returns a new DateOnly representing the last day of the specified quarter and
year, using the supplied calendar quarter definition.
public static DateOnly GetLastDateOfQuarter(int year, int quarter, CalendarQuarterDefinition definition)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive. If the quarter ends in the next calendar year (based on the definition's anchor), the year will be incremented accordingly.quarterintThe quarter number, from 1 through 4.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarters are aligned.
Returns
Remarks
The definition controls whether quarters are aligned to the first day of a month or anchored
to a custom day-of-month boundary.
Exceptions
- ArgumentOutOfRangeException
Thrown if
quarteris less than 1 or greater than 4, -or-definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use a provider-based overload instead.
GetLastDateOfWeekInMonth(int, int, DayOfWeek)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
in the given month and year.
public static DateOnly GetLastDateOfWeekInMonth(int year, int month, DayOfWeek dayOfWeek)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.monthintThe calendar month of the result, from 1 through 12.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the month. For example, Monday returns the last Monday.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the specified month and year.
Remarks
The search begins on the last day of the month and proceeds backward to locate the last matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-monthis less than 1 or greater than 12, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration.
GetLastDateOfWeekInQuarter(int, int, DayOfWeek)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the specified calendar quarter and year, using the standard
calendar quarter definition.
public static DateOnly GetLastDateOfWeekInQuarter(int year, int quarter, DayOfWeek dayOfWeek)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.quarterintThe quarter number, from 1 (Jan - Mar) through 4 (Oct - Dec).
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the last Monday.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the specified quarter and year.
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-quarteris less than 1 or greater than 4, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration.
GetLastDateOfWeekInQuarter(int, int, DayOfWeek, CalendarQuarterDefinition)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the specified quarter and year, using the supplied calendar
quarter definition.
public static DateOnly GetLastDateOfWeekInQuarter(int year, int quarter, DayOfWeek dayOfWeek, CalendarQuarterDefinition definition)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.quarterintThe quarter number, from 1 through 4.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the last Monday.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the specified quarter and year.
Remarks
The end of the quarter is computed using definition, and the search proceeds backward to the
last date that matches the specified dayOfWeek.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-quarteris less than 1 or greater than 4, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration, -or-definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
GetNearestDateOfWeek(int, int, int, DayOfWeek)
Returns a new DateOnly representing the nearest date (before or after) to the specified calendar
year, month, and day that falls on the given
DayOfWeek.
public static DateOnly GetNearestDateOfWeek(int year, int month, int day, DayOfWeek dayOfWeek)
Parameters
yearintThe calendar year of the reference date. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.monthintThe calendar month of the reference date. Must be between 1 and 12, inclusive, where 1 represents January and 12 represents December.
dayintThe day component of the reference date. Must be valid for the specified
yearandmonth, including leap-year considerations for February.dayOfWeekDayOfWeekThe target DayOfWeek to locate.
Returns
- DateOnly
A DateOnly value set to the closest date (either before or after) to the specified reference date that falls on the given
dayOfWeek. If two dates are equally close, the earlier one is returned.
Remarks
The result is computed by evaluating the day-distance between the specified reference date and the nearest
occurrence of dayOfWeek in either direction.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
GetNthDateOfWeekInMonth(int, int, DayOfWeek, WeekOrdinal)
Returns a new DateOnly representing the specified ordinal occurrence of a
DayOfWeek within the given calendar month and year.
public static DateOnly GetNthDateOfWeekInMonth(int year, int month, DayOfWeek dayOfWeek, WeekOrdinal ordinal)
Parameters
yearintThe calendar year of the result. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.monthintThe calendar month of the result. Must be between 1 and 12, inclusive, where 1 represents January and 12 represents December.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the month. For example, Tuesday returns the nth Tuesday.
ordinalWeekOrdinalThe ordinal occurrence to return. Valid values are First, Second, Third, Fourth, Fifth, and Last. Fifth is valid only in months where five matching weekdays occur.
Returns
- DateOnly
A DateOnly value set to the requested occurrence of
dayOfWeekwithin the specifiedyearandmonth.
Remarks
For Last, the method returns the final matching dayOfWeek in the
month. For other ordinal values, the method locates the first matching weekday and offsets by a multiple of
seven days to reach the desired ordinal.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-monthis less than 1 or greater than 12, -or-dayOfWeekis not a defined value of the DayOfWeek enumeration, -or-ordinalis not a defined value of the WeekOrdinal enumeration, -or- the requestedordinaldoes not occur within the month (for example, a fifth Thursday in February).
GetStartDateOfWeek(int, int, CultureInfo?)
Returns a new DateOnly representing the first day of the specified culture-defined week number in the given calendar year.
public static DateOnly GetStartDateOfWeek(int year, int week, CultureInfo? culture = null)
Parameters
yearintThe calendar year to evaluate. Must be between the
Yearproperty values of MinValue and MaxValue, inclusive.weekintThe culture-defined week number to evaluate, starting at 1. The maximum valid value depends on the CalendarWeekRule and DayOfWeek used by the supplied
culture.cultureCultureInfoAn optional CultureInfo used to determine the CalendarWeekRule and starting DayOfWeek. If null, CurrentCulture is used.
Returns
Remarks
This method uses the culture-defined week numbering system. The start of week 1 depends on the culture's CalendarWeekRule: under FirstDay the (possibly partial) first week begins on January 1 itself; under FirstFullWeek it begins at the first occurrence of the culture's FirstDayOfWeek on or after January 1; and under FirstFourDayWeek it begins at the week boundary of the week containing January 1 when at least four days of that week fall in the new year (which may place the start in the previous December), otherwise one week later. Subsequent weeks advance in 7-day intervals from the week-boundary alignment.
The result is validated by recalculating the week number for the computed date and comparing it to
week. Dates that fall in the previous calendar year (such as the start of ISO week 1 in late
December) are handled correctly. This member delegates to
GetStartDateOfWeek(int, int, CultureInfo?) - the twins share one
implementation, so both surfaces always agree.
Exceptions
- ArgumentOutOfRangeException
Thrown if
yearis less than theYearof MinValue or greater than that of MaxValue, -or-weekdoes not correspond to a valid week number foryearunder the rules of the supplied or currentculture.
IsFirstDateOfFiscalYear(DateOnly, IQuarterDefinitionProvider)
Returns true when date is the first day of its fiscal year under the
supplied IQuarterDefinitionProvider.
public static bool IsFirstDateOfFiscalYear(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date to test.
providerIQuarterDefinitionProviderThe provider that defines the fiscal year boundaries.
Returns
Exceptions
- ArgumentNullException
Thrown when
provideris null.
IsFirstDateOfMonth(DateOnly)
Determines whether the specified DateOnly falls on the first day of its calendar month.
public static bool IsFirstDateOfMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
Remarks
This method evaluates whether the Day component is equal to 1.
IsFirstDateOfQuarter(DateOnly)
Determines whether the specified DateOnly falls on the first day of its calendar quarter, using the standard calendar quarter definition.
public static bool IsFirstDateOfQuarter(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember: Q1 = Jan - Mar, Q2 = Apr - Jun, Q3 = Jul - Sep, Q4 = Oct - Dec.
IsFirstDateOfQuarter(DateOnly, CalendarQuarterDefinition)
Determines whether the specified DateOnly falls on the first day of its calendar quarter, using the supplied calendar quarter definition.
public static bool IsFirstDateOfQuarter(this DateOnly date, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value to evaluate.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown if
definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
IsFirstDateOfQuarter(DateOnly, IQuarterDefinitionProvider)
Determines whether the specified DateOnly falls on the first day of its calendar quarter, using a custom IQuarterDefinitionProvider.
public static bool IsFirstDateOfQuarter(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value to evaluate.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
Exceptions
- ArgumentNullException
Thrown if
provideris null.
IsInRange(DateOnly, DateOnly, DateOnly)
Determines whether the specified DateOnly falls within the inclusive range defined by
start and end.
public static bool IsInRange(this DateOnly date, DateOnly start, DateOnly end)
Parameters
dateDateOnlyThe date value to evaluate.
startDateOnlyThe inclusive lower bound of the range.
endDateOnlyThe inclusive upper bound of the range.
Returns
- bool
true if
dateis greater than or equal tostartand less than or equal toend; otherwise, false.
Remarks
IsInRange(DateOnly?, DateOnly, DateOnly)
Determines whether the specified nullable DateOnly falls within the inclusive range defined by
start and end.
public static bool IsInRange(this DateOnly? date, DateOnly start, DateOnly end)
Parameters
dateDateOnly?The nullable date value to evaluate.
startDateOnlyThe inclusive lower bound of the range.
endDateOnlyThe inclusive upper bound of the range.
Returns
- bool
true if
datehas a value that is greater than or equal tostartand less than or equal toend; otherwise, false .
Remarks
IsInWorkingWeek(DateOnly, WeekPattern)
Determines whether the specified DateOnly falls on a day that is selected in the supplied WeekPattern working week.
public static bool IsInWorkingWeek(this DateOnly date, WeekPattern workingWeek)
Parameters
dateDateOnlyThe date to evaluate.
workingWeekWeekPatternThe working-week pattern.
Returns
Remarks
This predicate considers only the day-of-week dimension. It does not consult any holiday catalogue. Combine it with a notable-date service when both working-week and holiday awareness are required.
IsInWorkingWeek(DateOnly, WorkingDaysOfWeek)
Determines whether the specified DateOnly falls on a day that is selected in the supplied WorkingDaysOfWeek working week.
public static bool IsInWorkingWeek(this DateOnly date, WorkingDaysOfWeek workingWeek)
Parameters
dateDateOnlyThe date to evaluate.
workingWeekWorkingDaysOfWeekThe named working-week pattern.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration.- ArgumentException
Thrown when
workingWeekis Custom, which has no canonical pattern.
IsLastDateOfFiscalYear(DateOnly, IQuarterDefinitionProvider)
Returns true when date is the last day of its fiscal year under the
supplied IQuarterDefinitionProvider.
public static bool IsLastDateOfFiscalYear(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date to test.
providerIQuarterDefinitionProviderThe provider that defines the fiscal year boundaries.
Returns
Exceptions
- ArgumentNullException
Thrown when
provideris null.
IsLastDateOfMonth(DateOnly)
Determines whether the specified DateOnly falls on the last day of its calendar month.
public static bool IsLastDateOfMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
Remarks
This method compares the Day component to the total number of days in the same month and year, accounting for leap-year adjustments to February.
Equivalent to checking whether date.Day == DateTime.DaysInMonth(date.Year, date.Month).
IsLastDateOfQuarter(DateOnly)
Determines whether the specified DateOnly falls on the last day of its calendar quarter, using the standard calendar quarter definition.
public static bool IsLastDateOfQuarter(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember: Q1 = Jan - Mar, Q2 = Apr - Jun, Q3 = Jul - Sep, Q4 = Oct - Dec.
IsLastDateOfQuarter(DateOnly, CalendarQuarterDefinition)
Determines whether the specified DateOnly falls on the last day of its calendar quarter, using the supplied calendar quarter definition.
public static bool IsLastDateOfQuarter(this DateOnly date, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value to evaluate.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown if
definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
IsLastDateOfQuarter(DateOnly, IQuarterDefinitionProvider)
Determines whether the specified DateOnly falls on the last day of its calendar quarter, using a custom IQuarterDefinitionProvider.
public static bool IsLastDateOfQuarter(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value to evaluate.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
Exceptions
- ArgumentNullException
Thrown if
provideris null.
IsLeapYear(DateOnly)
Determines whether the year of the specified DateOnly is a leap year, according to the proleptic Gregorian calendar.
public static bool IsLeapYear(this DateOnly date)
Parameters
Returns
Remarks
This method applies the Gregorian leap-year rules:
- Years divisible by 4 are leap years,
- except years divisible by 100,
- unless also divisible by 400.
For example, the years 2000 and 2024 are leap years, while 1900 and 2100 are not.
This method does not consider culture-specific calendars; it always evaluates leap years using the Gregorian calendar.
IsRestDay(DateOnly, WeekPattern)
Determines whether the specified DateOnly falls on a day that is not selected in the supplied WeekPattern working week.
public static bool IsRestDay(this DateOnly date, WeekPattern workingWeek)
Parameters
dateDateOnlyThe date to evaluate.
workingWeekWeekPatternThe working-week pattern.
Returns
Remarks
This predicate is the complement of IsInWorkingWeek(DateOnly, WeekPattern) and considers only the day-of-week dimension. It does not consult any holiday catalogue.
IsRestDay(DateOnly, WorkingDaysOfWeek)
Determines whether the specified DateOnly falls on a day that is not selected in the supplied WorkingDaysOfWeek working week.
public static bool IsRestDay(this DateOnly date, WorkingDaysOfWeek workingWeek)
Parameters
dateDateOnlyThe date to evaluate.
workingWeekWorkingDaysOfWeekThe named working-week pattern.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration.- ArgumentException
Thrown when
workingWeekis Custom, which has no canonical pattern.
IsWeekday(DateOnly)
Determines whether the specified DateOnly falls on a weekday, using the default MondayToFriday rule.
public static bool IsWeekday(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
Remarks
A weekday is any day selected by the working-week pattern. This overload uses MondayToFriday and no custom provider.
IsWeekday(DateOnly, WorkingDaysOfWeek, IWeekendDefinitionProvider?)
Determines whether the specified DateOnly falls on a weekday, using the supplied
WorkingDaysOfWeek and an optional custom provider.
public static bool IsWeekday(this DateOnly date, WorkingDaysOfWeek workingWeek, IWeekendDefinitionProvider? provider = null)
Parameters
dateDateOnlyThe date value to evaluate.
workingWeekWorkingDaysOfWeekThe WorkingDaysOfWeek that determines which days are treated as working days.
providerIWeekendDefinitionProviderAn optional IWeekendDefinitionProvider that supplies custom weekend logic when
workingWeekis Custom.
Returns
Remarks
The method evaluates whether the DayOfWeek of date is included in
the working-week pattern supplied by workingWeek and optionally refined by
provider.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration, -or-workingWeekis Custom andprovideris null.
IsWeekend(DateOnly)
Determines whether the specified DateOnly falls on a weekend, using the default MondayToFriday rule.
public static bool IsWeekend(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
Remarks
This overload uses the standard working-week pattern (Monday through Friday), so Saturday and Sunday are treated as weekend days.
IsWeekend(DateOnly, WorkingDaysOfWeek, IWeekendDefinitionProvider?)
Determines whether the specified DateOnly falls on a weekend, using the supplied
WorkingDaysOfWeek and an optional custom provider.
public static bool IsWeekend(this DateOnly date, WorkingDaysOfWeek workingWeek, IWeekendDefinitionProvider? provider = null)
Parameters
dateDateOnlyThe date value to evaluate.
workingWeekWorkingDaysOfWeekThe WorkingDaysOfWeek that determines which days are treated as working days. Any day not selected is treated as a weekend day.
providerIWeekendDefinitionProviderAn optional IWeekendDefinitionProvider that supplies custom weekend logic when
workingWeekis Custom.
Returns
- bool
true if
datefalls on a weekend day as defined by the supplied working-week or provider; otherwise, false.
Remarks
This method supports alternative working-week patterns used in different cultures and regions, such as Sunday-to-Thursday or Saturday-to-Wednesday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration, -or-workingWeekis Custom andprovideris null.
IsoWeekOfYear(DateOnly)
Returns the ISO 8601 week number for the specified date.
public static int IsoWeekOfYear(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
- int
An integer in the range 1 - 53 representing the ISO 8601 week number that contains
date.
Remarks
This method follows the ISO 8601 standard for week numbering, where:
- weeks begin on Monday;
- week 1 is the first week containing at least four days of the new year.
The result is computed using FirstFourDayWeek and Monday, and is identical to the value produced by the DateTime twin for the same calendar date.
IsoYear(DateOnly)
Returns the ISO 8601 year associated with the specified date.
public static int IsoYear(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
- int
The ISO 8601 calendar year that contains the ISO week of
date.
Remarks
The ISO 8601 year may differ from the calendar year of date. A date near the start or end of
a calendar year may belong to the ISO year of the adjacent calendar year, depending on which ISO week it falls
into. For example, January 1 may belong to the last week of the previous ISO year, and December 31 may belong to
week 1 of the following ISO year.
LastDateOfFiscalYear(int, IQuarterDefinitionProvider)
Returns the last calendar day of the supplied fiscal year under the supplied IQuarterDefinitionProvider.
public static DateOnly LastDateOfFiscalYear(int fiscalYear, IQuarterDefinitionProvider provider)
Parameters
fiscalYearintThe fiscal year whose end date is requested.
providerIQuarterDefinitionProviderThe provider that defines the fiscal year boundaries.
Returns
Exceptions
- ArgumentNullException
Thrown when
provideris null.
LastDateOfMonth(DateOnly)
Returns a new DateOnly representing the last day of the same calendar month and year as the
specified date.
public static DateOnly LastDateOfMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value whose year and month are used to determine the result.
Returns
Remarks
This method calculates the last day of the month using Gregorian calendar rules. Leap years are correctly accounted for when determining the length of February.
Example:
var date = new DateOnly(2024, 2, 15);
var result = date.LastDateOfMonth(); // → 2024-02-29
LastDateOfQuarter(DateOnly)
Returns a new DateOnly representing the last day of the calendar quarter that contains the
specified date, using the standard calendar quarter definition.
public static DateOnly LastDateOfQuarter(this DateOnly date)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
Returns
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember:
- Q1January - March
- Q2April - June
- Q3July - September
- Q4October - December
LastDateOfQuarter(DateOnly, CalendarQuarterDefinition)
Returns a new DateOnly representing the last day of the quarter that contains the specified
date, using the specified calendar quarter definition.
public static DateOnly LastDateOfQuarter(this DateOnly date, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
Remarks
The definition controls whether quarters are aligned to the first day of a month (e.g.
January - March) or anchored to a custom day-of-month boundary.
For provider-driven (e.g. 4-4-5 fiscal) quarters, use the LastDateOfQuarter(DateOnly, IQuarterDefinitionProvider) overload instead.
Exceptions
- ArgumentOutOfRangeException
Thrown if
definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
LastDateOfQuarter(DateOnly, IQuarterDefinitionProvider)
Returns a new DateOnly representing the last day of the quarter that contains the specified
date, using a custom IQuarterDefinitionProvider.
public static DateOnly LastDateOfQuarter(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
Remarks
This overload supports advanced or domain-specific quarter systems by delegating boundary logic to the supplied
provider - for example, 4-4-5 retail calendars or regional fiscal quarters.
Exceptions
- ArgumentNullException
Thrown if
provideris null.- ArgumentOutOfRangeException
Thrown if the
providerreturns a date outside the range of MinValue and MaxValue.
LastDateOfWeek(DateOnly)
Returns a new DateOnly representing the last day of the week that contains the specified
date, using the last day of the week defined by CurrentCulture.
public static DateOnly LastDateOfWeek(this DateOnly date)
Parameters
dateDateOnlyThe date value used to determine the containing week.
Returns
Remarks
This overload uses CurrentCulture to determine the last day of the week, inferred from FirstDayOfWeek.
LastDateOfWeek(DateOnly, WorkingDaysOfWeek)
Returns a new DateOnly representing the last day of the week that contains the specified
date, using a start-of-week inferred from the specified WorkingDaysOfWeek.
public static DateOnly LastDateOfWeek(this DateOnly date, WorkingDaysOfWeek workingWeek)
Parameters
dateDateOnlyThe date value used to determine the containing week.
workingWeekWorkingDaysOfWeekA WorkingDaysOfWeek used to infer the last day of the week. For example, MondayToFriday implies a Monday start (and therefore a Sunday end).
Returns
Remarks
The method infers the start of the week based on the specified workingWeek value, then
calculates the last day as six days after the inferred start. If AllDays is
supplied, the method defaults to using Monday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined WorkingDaysOfWeek value, -or- the resulting date is later than MaxValue.
LastDateOfWeek(DateOnly, CultureInfo?)
Returns a new DateOnly representing the last day of the week that contains the specified
date, using the last day of the week defined by the supplied or current culture.
public static DateOnly LastDateOfWeek(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value used to determine the containing week.
cultureCultureInfoAn optional CultureInfo that defines the first day of the week via FirstDayOfWeek. If null, CurrentCulture is used.
Returns
Remarks
This method computes the day offset between date and the culture-specific last day of the
week, and adds that offset.
Exceptions
- ArgumentOutOfRangeException
Thrown if the resulting date is later than MaxValue.
LastDateOfWeekInMonth(DateOnly, DayOfWeek)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the same calendar month and year as the specified date.
public static DateOnly LastDateOfWeekInMonth(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe date value whose month and year are used to determine the result.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the month. For example, Monday returns the last Monday.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the same calendar month and year asdate.
Remarks
The search begins on the last day of the month and proceeds backward to locate the last matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
LastDateOfWeekInQuarter(DateOnly, DayOfWeek)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the calendar quarter that contains the specified date, using the standard calendar
quarter definition.
public static DateOnly LastDateOfWeekInQuarter(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the last Monday.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the quarter that containsdate.
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember. The search begins on the last day of the quarter and proceeds backward to locate the last matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
LastDateOfWeekInQuarter(DateOnly, DayOfWeek, CalendarQuarterDefinition)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the quarter that contains the specified date, using the supplied calendar quarter
definition.
public static DateOnly LastDateOfWeekInQuarter(this DateOnly date, DayOfWeek dayOfWeek, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the last Monday.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how quarter boundaries are aligned.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the quarter that containsdate.
Remarks
The end of the quarter is computed using definition, and the search proceeds backward to the
last date that matches the specified dayOfWeek.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration, -or-definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
LastDateOfWeekInQuarter(DateOnly, DayOfWeek, IQuarterDefinitionProvider)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the quarter that contains the specified date, using a custom
IQuarterDefinitionProvider.
public static DateOnly LastDateOfWeekInQuarter(this DateOnly date, DayOfWeek dayOfWeek, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value used to determine the containing quarter.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the quarter. For example, Monday returns the last Monday.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the quarter that containsdate.
Remarks
The end of the quarter is determined by the supplied provider, and the search proceeds
backward to the last date that matches the specified dayOfWeek.
Exceptions
- ArgumentNullException
Thrown if
provideris null.- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
LastDateOfWeekInYear(DateOnly, DayOfWeek)
Returns a new DateOnly representing the last occurrence of the specified DayOfWeek
within the same calendar year as the specified date.
public static DateOnly LastDateOfWeekInYear(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe date value whose year is used to determine the result.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the year. For example, Monday returns the last Monday.
Returns
- DateOnly
A DateOnly value set to the last occurrence of
dayOfWeekwithin the same calendar year asdate.
Remarks
The search begins on December 31 of the year and proceeds backward to locate the last matching weekday.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
LastDateOfYear(DateOnly)
Returns a new DateOnly representing the last day of the same calendar year as the specified
date.
public static DateOnly LastDateOfYear(this DateOnly date)
Parameters
dateDateOnlyThe date value whose year is used to determine the result.
Returns
Remarks
This method calculates the last day of the year using Gregorian calendar rules.
Example:
var date = new DateOnly(2025, 7, 15);
var result = date.LastDateOfYear(); // → 2025-12-31
Max(DateOnly, DateOnly)
Returns the later of two specified DateOnly values.
public static DateOnly Max(DateOnly first, DateOnly second)
Parameters
firstDateOnlyThe first DateOnly value to compare.
secondDateOnlyThe second DateOnly value to compare.
Returns
Remarks
This method compares the two values using the greater-than-or-equal-to (>=) operator, which is
equivalent to CompareTo(DateOnly).
Max(DateOnly?, DateOnly?)
Returns the later of two specified nullable DateOnly values.
public static DateOnly? Max(DateOnly? first, DateOnly? second)
Parameters
firstDateOnly?The first nullable DateOnly value to compare.
secondDateOnly?The second nullable DateOnly value to compare.
Returns
Remarks
Min(DateOnly, DateOnly)
Returns the earlier of two specified DateOnly values.
public static DateOnly Min(DateOnly first, DateOnly second)
Parameters
firstDateOnlyThe first DateOnly value to compare.
secondDateOnlyThe second DateOnly value to compare.
Returns
Remarks
This method compares the two values using the less-than-or-equal-to (<=) operator, which is equivalent
to CompareTo(DateOnly).
Min(DateOnly?, DateOnly?)
Returns the earlier of two specified nullable DateOnly values.
public static DateOnly? Min(DateOnly? first, DateOnly? second)
Parameters
firstDateOnly?The first nullable DateOnly value to compare.
secondDateOnly?The second nullable DateOnly value to compare.
Returns
Remarks
MonthName(DateOnly)
Returns the full name of the month for the specified DateOnly, using the formatting rules of CurrentCulture.
public static string MonthName(this DateOnly date)
Parameters
dateDateOnlyThe date value whose month component is used to determine the name.
Returns
- string
A string containing the localized full month name, formatted using CurrentCulture.
Remarks
This overload uses the GetMonthName(int) method of the current culture to retrieve the month name. For culture-specific results, use the MonthName(DateOnly, CultureInfo?) overload.
MonthName(DateOnly, CultureInfo?)
Returns the full name of the month for the specified DateOnly, using the formatting rules of the supplied or current culture.
public static string MonthName(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value whose month component is used to determine the name.
cultureCultureInfoAn optional CultureInfo used to format the result. If null, CurrentCulture is used.
Returns
- string
A string containing the localized full month name for
date, formatted using the supplied or current culture.
Remarks
This overload uses the GetMonthName(int) method of the supplied or current culture to retrieve the month name.
NearestDateOfWeek(DateOnly, DayOfWeek)
Returns a new DateOnly representing the nearest date (before or after) to the specified
date that falls on the given DayOfWeek.
public static DateOnly NearestDateOfWeek(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
Returns
- DateOnly
A DateOnly value set to the closest date (either before or after) to
datethat falls on the specifieddayOfWeek. If two dates are equally close, the earlier one is returned.
Remarks
The result is computed by evaluating the day-distance between date and the nearest
occurrence of dayOfWeek in either direction.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
NextDateOfWeek(DateOnly, DayOfWeek)
Returns a new DateOnly representing the next calendar occurrence of the specified
DayOfWeek after the given date.
public static DateOnly NextDateOfWeek(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe starting date value from which to search forward.
dayOfWeekDayOfWeekThe DayOfWeek to locate. For example, Monday returns the next Monday.
Returns
Remarks
If date already falls on the specified dayOfWeek, the result is exactly
seven days later. The method advances forward in time and never returns the original date.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
NextOccurrence(DateOnly, int, DateOnly)
Returns a new DateOnly representing the next occurrence of a recurring event that starts at
start and repeats every intervalDays days, occurring on or after the
specified after date.
public static DateOnly NextOccurrence(this DateOnly start, int intervalDays, DateOnly after)
Parameters
startDateOnlyThe date value representing the initial reference point of the recurring event.
intervalDaysintThe fixed number of days between successive occurrences. Must be greater than zero.
afterDateOnlyThe date after which the next occurrence must fall.
Returns
- DateOnly
A DateOnly value representing the next occurrence of the event aligned with
startand recurring everyintervalDaysdays, on or afterafter.
Remarks
If after is earlier than start, the method returns
start. Otherwise, it computes the smallest multiple of intervalDays
added to start that occurs after after.
Example:
var start = new DateOnly(2024, 1, 1);
var after = new DateOnly(2024, 1, 12);
var next = start.NextOccurrence(intervalDays: 5, after); // → 2024-01-16
Exceptions
- ArgumentOutOfRangeException
Thrown if
intervalDaysis less than or equal to zero.
NextOrSameDateOfWeek(DateOnly, DayOfWeek)
Returns a new DateOnly representing the next calendar occurrence of the specified
DayOfWeek at or after the given date.
public static DateOnly NextOrSameDateOfWeek(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe starting date value from which to search forward.
dayOfWeekDayOfWeekThe DayOfWeek to locate. For example, Monday returns the next Monday on or after
date.
Returns
Remarks
If date already falls on the specified dayOfWeek, the result is
date itself. This is the on-or-after counterpart of
NextDateOfWeek(DateOnly, DayOfWeek), which always advances by at least one day.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
NextWeekday(DateOnly, WeekPattern)
Returns a new DateOnly representing the next day after date whose
DayOfWeek is selected in the supplied workingWeek.
public static DateOnly NextWeekday(this DateOnly date, WeekPattern workingWeek)
Parameters
dateDateOnlyThe starting date value from which to search forward.
workingWeekWeekPatternThe working-week pattern that determines which days are considered working days.
Returns
- DateOnly
The first calendar day strictly after
datewhose day-of-week is selected inworkingWeek.
Exceptions
- ArgumentOutOfRangeException
Thrown when
workingWeekis Empty.
NextWeekday(DateOnly, WorkingDaysOfWeek)
Returns a new DateOnly representing the next calendar weekday after the specified
date, based on the supplied workingWeek pattern.
public static DateOnly NextWeekday(this DateOnly date, WorkingDaysOfWeek workingWeek)
Parameters
dateDateOnlyThe starting date value from which to search forward.
workingWeekWorkingDaysOfWeekThe WorkingDaysOfWeek that determines which days are treated as working days.
Returns
- DateOnly
A DateOnly value set to the first calendar day after
datethat is a working day under the specifiedworkingWeekrule.
Remarks
The method evaluates each successive day until it finds one that is selected as a working day by the specified
rule. The original date is never returned, even if it already falls on a working day.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration.
NextWeekday(DateOnly, WorkingDaysOfWeek, IWeekendDefinitionProvider?)
Returns a new DateOnly representing the next calendar weekday after the specified
date, using the supplied workingWeek pattern and an optional custom
provider.
public static DateOnly NextWeekday(this DateOnly date, WorkingDaysOfWeek workingWeek, IWeekendDefinitionProvider? provider)
Parameters
dateDateOnlyThe starting date value from which to search forward.
workingWeekWorkingDaysOfWeekThe WorkingDaysOfWeek that determines which days are treated as working days.
providerIWeekendDefinitionProviderAn optional IWeekendDefinitionProvider that supplies custom weekend logic when
workingWeekis Custom. If null, the default behavior for the suppliedworkingWeekapplies.
Returns
- DateOnly
A DateOnly value set to the first calendar day after
datethat is a working day under the specifiedworkingWeekrule and the logic ofprovider.
Remarks
The method evaluates each successive day following date until it finds one that is a working
day, either by the supplied workingWeek pattern or by the custom logic of
provider.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration.
NthDateOfWeekInMonth(DateOnly, DayOfWeek, WeekOrdinal)
Returns a new DateOnly representing the specified ordinal occurrence of a
DayOfWeek within the same calendar month and year as the specified date.
public static DateOnly NthDateOfWeekInMonth(this DateOnly date, DayOfWeek dayOfWeek, WeekOrdinal ordinal)
Parameters
dateDateOnlyThe date value whose month and year are used to determine the result. The day component is ignored.
dayOfWeekDayOfWeekThe DayOfWeek to locate within the month. For example, Monday returns the nth Monday.
ordinalWeekOrdinalThe ordinal occurrence to return. Valid values are First, Second, Third, Fourth, Fifth, and Last. Fifth is valid only in months where five matching weekdays occur.
Returns
- DateOnly
A DateOnly value set to the requested occurrence of
dayOfWeekwithin the same calendar month and year asdate.
Remarks
For Last, the method returns the final matching dayOfWeek in the
month. For other ordinal values, the method locates the first matching weekday and offsets by a multiple of
seven days to reach the desired ordinal.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration, -or-ordinalis not a defined value of the WeekOrdinal enumeration, -or- the requestedordinaldoes not occur within the month (for example, a fifth Thursday in February).
PreviousDateOfWeek(DateOnly, DayOfWeek)
Returns a new DateOnly representing the previous calendar occurrence of the specified
DayOfWeek before the given date.
public static DateOnly PreviousDateOfWeek(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe starting date value from which to search backward.
dayOfWeekDayOfWeekThe DayOfWeek to locate. For example, Monday returns the previous Monday.
Returns
Remarks
If date already falls on the specified dayOfWeek, the result is exactly
seven days earlier. The method moves backward in time and never returns the original date.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
PreviousOccurrence(DateOnly, int, DateOnly)
Returns a new DateOnly representing the previous occurrence of a recurring event that starts at
start and repeats every intervalDays days, occurring strictly before the
specified before date.
public static DateOnly PreviousOccurrence(this DateOnly start, int intervalDays, DateOnly before)
Parameters
startDateOnlyThe date value representing the initial reference point of the recurring event.
intervalDaysintThe fixed number of days between successive occurrences. Must be greater than zero.
beforeDateOnlyThe date before which the previous occurrence must fall.
Returns
- DateOnly
A DateOnly value representing the last occurrence of the event that falls strictly before
before, based on the suppliedstartand recurringintervalDays.
Remarks
If before is earlier than or equal to start, the method returns the
occurrence immediately prior to start. Otherwise, it computes the largest multiple of
intervalDays added to start that remains strictly earlier than
before. When before falls exactly on an occurrence boundary, the
occurrence at that boundary is excluded and the preceding one is returned.
Example:
var start = new DateOnly(2024, 1, 1);
// before is between two occurrences - returns the occurrence at 2024-01-11
var prev1 = start.PreviousOccurrence(intervalDays: 5, new DateOnly(2024, 1, 13)); // → 2024-01-11
// before falls exactly on an occurrence - returns the one before it
var prev2 = start.PreviousOccurrence(intervalDays: 5, new DateOnly(2024, 1, 11)); // → 2024-01-06
Exceptions
- ArgumentOutOfRangeException
Thrown if
intervalDaysis less than or equal to zero.
PreviousOrSameDateOfWeek(DateOnly, DayOfWeek)
Returns a new DateOnly representing the previous calendar occurrence of the specified
DayOfWeek at or before the given date.
public static DateOnly PreviousOrSameDateOfWeek(this DateOnly date, DayOfWeek dayOfWeek)
Parameters
dateDateOnlyThe starting date value from which to search backward.
dayOfWeekDayOfWeekThe DayOfWeek to locate. For example, Monday returns the previous Monday on or before
date.
Returns
Remarks
If date already falls on the specified dayOfWeek, the result is
date itself. This is the on-or-before counterpart of
PreviousDateOfWeek(DateOnly, DayOfWeek), which always retreats by at least one day.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayOfWeekis not a defined value of the DayOfWeek enumeration.
PreviousWeekday(DateOnly, WeekPattern)
Returns a new DateOnly representing the previous day before date whose
DayOfWeek is selected in the supplied workingWeek.
public static DateOnly PreviousWeekday(this DateOnly date, WeekPattern workingWeek)
Parameters
dateDateOnlyThe starting date value from which to search backward.
workingWeekWeekPatternThe working-week pattern that determines which days are considered working days.
Returns
- DateOnly
The first calendar day strictly before
datewhose day-of-week is selected inworkingWeek.
Exceptions
- ArgumentOutOfRangeException
Thrown when
workingWeekis Empty.
PreviousWeekday(DateOnly, WorkingDaysOfWeek)
Returns a new DateOnly representing the previous calendar weekday before the specified
date, based on the supplied workingWeek pattern.
public static DateOnly PreviousWeekday(this DateOnly date, WorkingDaysOfWeek workingWeek)
Parameters
dateDateOnlyThe starting date value from which to search backward.
workingWeekWorkingDaysOfWeekThe WorkingDaysOfWeek that determines which days are treated as working days.
Returns
- DateOnly
A DateOnly value set to the first calendar day before
datethat is a working day under the specifiedworkingWeekrule.
Remarks
The method evaluates each preceding day until it finds one that is selected as a working day by the specified
rule. The original date is never returned, even if it already falls on a working day.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration.
PreviousWeekday(DateOnly, WorkingDaysOfWeek, IWeekendDefinitionProvider?)
Returns a new DateOnly representing the previous calendar weekday before the specified
date, using the supplied workingWeek pattern and an optional custom
provider.
public static DateOnly PreviousWeekday(this DateOnly date, WorkingDaysOfWeek workingWeek, IWeekendDefinitionProvider? provider)
Parameters
dateDateOnlyThe starting date value from which to search backward.
workingWeekWorkingDaysOfWeekThe WorkingDaysOfWeek that determines which days are treated as working days.
providerIWeekendDefinitionProviderAn optional IWeekendDefinitionProvider that supplies custom weekend logic when
workingWeekis Custom. If null, the default behavior for the suppliedworkingWeekapplies.
Returns
- DateOnly
A DateOnly value set to the first calendar day before
datethat is a working day under the specifiedworkingWeekrule and the logic ofprovider.
Remarks
The method evaluates each preceding day prior to date until it finds one that is a working
day, either by the supplied workingWeek pattern or by the custom logic of
provider.
Exceptions
- ArgumentOutOfRangeException
Thrown if
workingWeekis not a defined value of the WorkingDaysOfWeek enumeration.
Quarter(DateOnly)
Returns the quarter number (1 - 4) of the year for the specified DateOnly, using the standard calendar quarter definition.
public static int Quarter(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
- int
An integer between 1 and 4 representing the calendar quarter that contains
date.
Remarks
This overload uses the standard calendar alignment defined by JanuaryToDecember: Q1 = Jan - Mar, Q2 = Apr - Jun, Q3 = Jul - Sep, Q4 = Oct - Dec.
Quarter(DateOnly, CalendarQuarterDefinition)
Returns the quarter number (1 - 4) for the specified DateOnly, using the supplied calendar quarter definition.
public static int Quarter(this DateOnly date, CalendarQuarterDefinition definition)
Parameters
dateDateOnlyThe date value to evaluate.
definitionCalendarQuarterDefinitionThe CalendarQuarterDefinition that determines how the year is segmented into quarters.
Returns
- int
An integer between 1 and 4 representing the quarter that contains
date.
Remarks
This overload supports both month-aligned and day-aligned quarter definitions. For provider-driven custom calendars (e.g. 4-4-5 retail calendars), use the Quarter(DateOnly, IQuarterDefinitionProvider) overload.
Exceptions
- ArgumentOutOfRangeException
Thrown if
definitionis not a defined value of the CalendarQuarterDefinition enumeration.- InvalidOperationException
Thrown if
definitionis Custom; use the provider-based overload instead.
Quarter(DateOnly, IQuarterDefinitionProvider)
Returns the quarter number (1 - 4) for the specified DateOnly, using a custom IQuarterDefinitionProvider.
public static int Quarter(this DateOnly date, IQuarterDefinitionProvider provider)
Parameters
dateDateOnlyThe date value to evaluate.
providerIQuarterDefinitionProviderThe IQuarterDefinitionProvider that defines custom quarter boundaries. Must not be null.
Returns
- int
An integer between 1 and 4 representing the quarter that contains
date.
Remarks
This overload supports advanced or domain-specific quarter systems by delegating to GetQuarter(DateOnly) - for example, 4-4-5 retail calendars or regional fiscal quarters.
Exceptions
- ArgumentNullException
Thrown if
provideris null.- ArgumentOutOfRangeException
Thrown if the value returned by
provideris not in the range 1 - 4.
ToUnixTimeMilliseconds(DateOnly)
Returns the number of milliseconds that have elapsed between the Unix epoch (1970-01-01T00:00:00Z) and the specified DateOnly.
public static long ToUnixTimeMilliseconds(this DateOnly date)
Parameters
dateDateOnlyThe date value to convert. The value is interpreted as UTC.
Returns
- long
The total number of milliseconds since the Unix epoch.
Remarks
Use FromUnixTimeMilliseconds(long) to perform the inverse conversion.
- See Also
ToUnixTimeSeconds(DateOnly)
Returns the number of seconds that have elapsed between the Unix epoch (1970-01-01T00:00:00Z) and the specified DateOnly.
public static long ToUnixTimeSeconds(this DateOnly date)
Parameters
dateDateOnlyThe date value to convert. The value is interpreted as UTC.
Returns
- long
The total number of seconds since the Unix epoch.
Remarks
Use FromUnixTimeSeconds(long) to perform the inverse conversion.
- See Also
WeekOfMonth(DateOnly)
Returns the 1-based week number of the month for the specified DateOnly, using the CalendarWeekRule and DayOfWeek settings of CurrentCulture.
public static int WeekOfMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
- int
An integer indicating the week of the month in which
datefalls, starting at1.
Remarks
Week numbering is determined by CurrentCulture, specifically its CalendarWeekRule and FirstDayOfWeek. See WeekOfMonth(DateOnly, CalendarWeekRule, DayOfWeek) for the precise semantics of each rule, including the treatment of dates that precede week 1 of their month.
WeekOfMonth(DateOnly, CalendarWeekRule, DayOfWeek)
Returns the 1-based week number of the month for the specified DateOnly, using the supplied CalendarWeekRule and DayOfWeek as the week-starting day.
public static int WeekOfMonth(this DateOnly date, CalendarWeekRule weekRule, DayOfWeek weekStart)
Parameters
dateDateOnlyThe date value to evaluate.
weekRuleCalendarWeekRuleThe CalendarWeekRule that defines how the first week of the year is identified.
weekStartDayOfWeekThe DayOfWeek on which each week begins.
Returns
- int
An integer indicating the week of the month in which
datefalls, starting at1. Under FirstFullWeek and FirstFourDayWeek, dates that precede week 1 of their month return the week number they carry in the previous month (see remarks).
Remarks
The supplied weekRule determines where week 1 of the month begins, mirroring the semantics
the GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) family applies to years:
FirstDay - week 1 begins on the first day of the month, however short that
partial week is; each subsequent week begins on the next weekStart.
FirstFullWeek - week 1 begins on the first weekStart on or
after the first day of the month. Dates before that boundary belong to the trailing week of the previous month
and return that week's number (for example, 1 March 2024 with a Sunday week start returns 4, the week
number of the week beginning Sunday 25 February).
FirstFourDayWeek - the week containing the first day of the month is week 1 when
at least four of its days fall in that month; otherwise week 1 begins on the following
weekStart and the leading dates resolve to the previous month's trailing week, as for
FirstFullWeek.
The result is therefore never less than 1, but it is not always the week of the date's own month.
Exceptions
- ArgumentOutOfRangeException
Thrown if
weekRuleis not a defined value of the CalendarWeekRule enumeration, -or-weekStartis not a defined value of the DayOfWeek enumeration.
WeekOfMonth(DateOnly, CultureInfo?)
Returns the 1-based week number of the month for the specified DateOnly, using the calendar settings of the supplied or current culture.
public static int WeekOfMonth(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value to evaluate.
cultureCultureInfoAn optional CultureInfo that supplies the CalendarWeekRule and DayOfWeek settings. If null, CurrentCulture is used.
Returns
- int
An integer indicating the week of the month in which
datefalls, starting at1.
Remarks
This overload uses the supplied culture's CalendarWeekRule and FirstDayOfWeek to compute the result. See WeekOfMonth(DateOnly, CalendarWeekRule, DayOfWeek) for the precise semantics of each rule, including the treatment of dates that precede week 1 of their month.
WeekOfYear(DateOnly)
Returns the 1-based week number of the year that contains the specified DateOnly, using the CalendarWeekRule and DayOfWeek settings of CurrentCulture.
public static int WeekOfYear(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
- int
An integer in the range 1 - 53 representing the week of the year that contains
date.
Remarks
Week numbering is determined by CurrentCulture, which may follow different conventions:
- U.S. system: week 1 starts on Sunday and includes January 1.
- ISO 8601: week 1 starts on Monday and includes the first Thursday of the year.
WeekOfYear(DateOnly, CalendarWeekRule, DayOfWeek)
Returns the 1-based week number of the year that contains the specified DateOnly, using the supplied CalendarWeekRule and DayOfWeek as the week-starting day.
public static int WeekOfYear(this DateOnly date, CalendarWeekRule weekRule, DayOfWeek weekStart)
Parameters
dateDateOnlyThe date value to evaluate.
weekRuleCalendarWeekRuleThe CalendarWeekRule that defines how the first week of the year is identified.
weekStartDayOfWeekThe DayOfWeek on which each week begins.
Returns
- int
An integer in the range 1 - 53 representing the week of the year that contains
date.
Remarks
This overload enables custom calendar logic such as ISO 8601 (FirstFourDayWeek, Monday) or localized U.S./European systems.
Exceptions
- ArgumentOutOfRangeException
Thrown if
weekRuleis not a defined value of the CalendarWeekRule enumeration, -or-weekStartis not a defined value of the DayOfWeek enumeration.
WeekOfYear(DateOnly, CultureInfo?)
Returns the 1-based week number of the year that contains the specified DateOnly, using the calendar rules of the supplied or current culture.
public static int WeekOfYear(this DateOnly date, CultureInfo? culture)
Parameters
dateDateOnlyThe date value to evaluate.
cultureCultureInfoAn optional CultureInfo that supplies the CalendarWeekRule and DayOfWeek settings. If null, CurrentCulture is used.
Returns
- int
An integer in the range 1 - 53 representing the week of the year that contains
date.
Remarks
This overload allows culture-specific calculation of week numbers (e.g. for Gregorian or ISO 8601 calendars).
WeekOrdinalOfMonth(DateOnly)
Returns the WeekOrdinal represented by the specified DateOnly, indicating the ordinal occurrence of its DayOfWeek within the month.
public static WeekOrdinal WeekOrdinalOfMonth(this DateOnly date)
Parameters
dateDateOnlyThe date value to evaluate.
Returns
- WeekOrdinal
A WeekOrdinal value indicating which occurrence of the weekday
daterepresents within its calendar month.
Remarks
The result is calculated by counting how many full seven-day intervals have passed since the start of the month, based on Day. For example, the 1st through 7th of the month yield First, while the 8th through 14th yield Second, and so on.
Fifth is only returned when a month contains five occurrences of the given DayOfWeek.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |