DateTimeFormatInfoExtensions Class
Definition
Provides culture-aware calendar lookups against DateTimeFormatInfo that derive week-structure values which the BCL exposes only indirectly.
public static class DateTimeFormatInfoExtensions
- Inheritance
-
DateTimeFormatInfoExtensions
- Inherited Members
Remarks
DateTimeFormatInfo publishes the first day of the week through FirstDayOfWeek, but offers no symmetrical accessor for the last day of the week - yet that value is needed for week-end snapping, weekend predicates, and report headers in any locale-driven calendar code. This class supplies the missing accessors, derived from the same culture data so the result matches whatever the active culture says is the start of the week.
The API surface is intentionally small and is shaped around culture-driven derivations rather than free-form
formatting: the central helper is LastDayOfWeek, which returns the DayOfWeek immediately
preceding FirstDayOfWeek in the cyclic week.
All methods are pure, allocation-free, and reject a null DateTimeFormatInfo with ArgumentNullException. Results are sensitive to the supplied DateTimeFormatInfo - they intentionally do not consult ambient culture state - so callers can rely on deterministic behavior even when the current thread's culture changes between calls.
var enGb = CultureInfo.GetCultureInfo("en-GB").DateTimeFormat;
var enUs = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
DayOfWeek lastInBritain = enGb.LastDayOfWeek();
// => DayOfWeek.Sunday (en-GB starts on Monday, so the week ends on Sunday)
DayOfWeek lastInAmerica = enUs.LastDayOfWeek();
// => DayOfWeek.Saturday (en-US starts on Sunday, so the week ends on Saturday)
Methods
LastDayOfWeek(DateTimeFormatInfo)
Returns the last day of the week based on the specified DateTimeFormatInfo.
public static DayOfWeek LastDayOfWeek(this DateTimeFormatInfo info)
Parameters
infoDateTimeFormatInfoThe DateTimeFormatInfo containing week configuration.
Returns
Exceptions
- ArgumentNullException
Thrown if
infois null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |