Table of Contents

Class DateTimeExtensions

Namespace
Ddth.Utilities.Tempus
Assembly
Ddth.Utilities.dll

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

dateTime DateTime
dows IEnumerable<DayOfWeek>

Returns

bool

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

dateTime DateTime
dows IEnumerable<string>

Returns

bool

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

dateTime DateTime
dows DayOfWeek[]

Returns

bool

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

dateTime DateTime
dows string[]

Returns

bool

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

dateTime DateTime
startInclusive TimeOnly
endExclusive TimeOnly

Returns

bool

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

dateTime DateTime

Returns

DateTime

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

dateTime DateTime

Returns

DateTime

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

dateTime DateTime

Returns

DateTime

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

dateTime DateTime

Returns

DateTime

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

dateTime DateTime
firstMonthOfFiscalYear int

The month that starts the fiscal year (1–12). Defaults to 1 (January).

Returns

DateTime

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

dateTime DateTime

Returns

DateTime

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

dateTime DateTime

Returns

DateTime

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

dateTime DateTime
firstDayOfWeek DayOfWeek

The day considered the start of the week. Defaults to Monday.

Returns

DateTime