Class DateTimeExtensions
Extensions that provide utility methods for the DateTime struct.
public static class DateTimeExtensions
- Inheritance
-
DateTimeExtensions
- Inherited Members
Methods
IsOnDayOfWeek(DateTime, IEnumerable<DayOfWeek>)
Checks if the day of week component of the given DateTime falls on any of the specified days of week.
public static bool IsOnDayOfWeek(this DateTime dateTime, IEnumerable<DayOfWeek> dows)
Parameters
dateTimeDateTimedowsIEnumerable<DayOfWeek>
Returns
IsOnDayOfWeek(DateTime, IEnumerable<string>)
Checks if the day of week component of the given DateTime falls on any of the specified days of week (case-insensitive, supports both full and abbreviated day of week names).
public static bool IsOnDayOfWeek(this DateTime dateTime, IEnumerable<string> dows)
Parameters
dateTimeDateTimedowsIEnumerable<string>
Returns
IsOnDayOfWeek(DateTime, params DayOfWeek[])
Checks if the day of week component of the given DateTime falls on any of the specified days of week.
public static bool IsOnDayOfWeek(this DateTime dateTime, params DayOfWeek[] dows)
Parameters
Returns
IsOnDayOfWeek(DateTime, params string[])
Checks if the day of week component of the given DateTime falls on any of the specified days of week (case-insensitive, supports both full and abbreviated day of week names).
public static bool IsOnDayOfWeek(this DateTime dateTime, params string[] dows)
Parameters
Returns
IsWithinTimeWindow(DateTime, TimeOnly, TimeOnly)
Checks if the time component of the given DateTime falls within the specified time window (e.g. start <= dateTime < end).
public static bool IsWithinTimeWindow(this DateTime dateTime, TimeOnly startInclusive, TimeOnly endExclusive)
Parameters
Returns
Remarks
The time window can span across midnight. For example, a time window from 22:00 to 02:00 will include times from 22:00 to 23:59 and from 00:00 to 02:00.
NextWeekDay(DateTime)
Returns a new DateTime representing the next weekday (Monday to Friday) of the given DateTime.
public static DateTime NextWeekDay(this DateTime dateTime)
Parameters
dateTimeDateTime
Returns
PrevWeekDay(DateTime)
Returns a new DateTime representing the previous weekday (Monday to Friday) of the given DateTime.
public static DateTime PrevWeekDay(this DateTime dateTime)
Parameters
dateTimeDateTime
Returns
StartOfCalendarYear(DateTime)
Returns a new DateTime set to 00:00:00 on January 1st of the same year, preserving Kind.
public static DateTime StartOfCalendarYear(this DateTime dateTime)
Parameters
dateTimeDateTime
Returns
StartOfDay(DateTime)
Returns a new DateTime with the time component set to 00:00:00, preserving Kind.
public static DateTime StartOfDay(this DateTime dateTime)
Parameters
dateTimeDateTime
Returns
StartOfFiscalYear(DateTime, int)
Returns a new DateTime set to 00:00:00 on the 1st day of the fiscal year, preserving Kind.
public static DateTime StartOfFiscalYear(this DateTime dateTime, int firstMonthOfFiscalYear = 1)
Parameters
dateTimeDateTimefirstMonthOfFiscalYearintThe month that starts the fiscal year (1–12). Defaults to 1 (January).
Returns
StartOfMonth(DateTime)
Returns a new DateTime set to 00:00:00 on the 1st day of the month, preserving Kind.
public static DateTime StartOfMonth(this DateTime dateTime)
Parameters
dateTimeDateTime
Returns
StartOfQuarter(DateTime)
Returns a new DateTime set to 00:00:00 on the 1st day of the quarter (Q1=Jan, Q2=Apr, Q3=Jul, Q4=Oct), preserving Kind.
public static DateTime StartOfQuarter(this DateTime dateTime)
Parameters
dateTimeDateTime
Returns
StartOfWeek(DateTime, DayOfWeek)
Returns a new DateTime set to 00:00:00 on the first day of the week, preserving Kind.
public static DateTime StartOfWeek(this DateTime dateTime, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
Parameters
dateTimeDateTimefirstDayOfWeekDayOfWeekThe day considered the start of the week. Defaults to Monday.