Table of Contents

Class DateTimeOffsetExtensions

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

Extensions that provide utility methods for the DateTimeOffset struct.

public static class DateTimeOffsetExtensions
Inheritance
DateTimeOffsetExtensions
Inherited Members

Methods

AsTimeZoneSilently(DateTimeOffset, string)

Shifts the given DateTimeOffset to the specified time zone (e.g. keep date and time, change only the time zone). Returns null if the time zone ID is invalid or not found.

public static DateTimeOffset? AsTimeZoneSilently(this DateTimeOffset dateTime, string tzId)

Parameters

dateTime DateTimeOffset
tzId string

Returns

DateTimeOffset?

IsOnDayOfWeek(DateTimeOffset, IEnumerable<DayOfWeek>)

Checks if the day of week component of the given DateTimeOffset falls on any of the specified days of week.

public static bool IsOnDayOfWeek(this DateTimeOffset dateTime, IEnumerable<DayOfWeek> dows)

Parameters

dateTime DateTimeOffset
dows IEnumerable<DayOfWeek>

Returns

bool

IsOnDayOfWeek(DateTimeOffset, IEnumerable<string>)

Checks if the day of week component of the given DateTimeOffset 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 DateTimeOffset dateTime, IEnumerable<string> dows)

Parameters

dateTime DateTimeOffset
dows IEnumerable<string>

Returns

bool

IsOnDayOfWeek(DateTimeOffset, params DayOfWeek[])

Checks if the day of week component of the given DateTimeOffset falls on any of the specified days of week.

public static bool IsOnDayOfWeek(this DateTimeOffset dateTime, params DayOfWeek[] dows)

Parameters

dateTime DateTimeOffset
dows DayOfWeek[]

Returns

bool

IsOnDayOfWeek(DateTimeOffset, params string[])

Checks if the day of week component of the given DateTimeOffset 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 DateTimeOffset dateTime, params string[] dows)

Parameters

dateTime DateTimeOffset
dows string[]

Returns

bool

IsWithinTimeWindow(DateTimeOffset, TimeOnly, TimeOnly)

Checks if the time component of the given DateTimeOffset falls within the specified time window (e.g. start <= dateTime < end).

public static bool IsWithinTimeWindow(this DateTimeOffset dateTime, TimeOnly startInclusive, TimeOnly endExclusive)

Parameters

dateTime DateTimeOffset
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(DateTimeOffset)

Returns a new DateTimeOffset representing the next weekday (Monday to Friday) of the given DateTimeOffset.

public static DateTimeOffset NextWeekDay(this DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset

Returns

DateTimeOffset

PrevWeekDay(DateTimeOffset)

Returns a new DateTimeOffset representing the previous weekday (Monday to Friday) of the given DateTimeOffset.

public static DateTimeOffset PrevWeekDay(this DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset

Returns

DateTimeOffset

StartOfCalendarYear(DateTimeOffset)

Returns a new DateTimeOffset set to 00:00:00 on January 1st of the same year, preserving Offset.

public static DateTimeOffset StartOfCalendarYear(this DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset

Returns

DateTimeOffset

StartOfDay(DateTimeOffset)

Returns a new DateTimeOffset with the time component set to 00:00:00, preserving Kind.

public static DateTimeOffset StartOfDay(this DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset

Returns

DateTimeOffset

StartOfFiscalYear(DateTimeOffset, int)

Returns a new DateTimeOffset set to 00:00:00 on the 1st day of the fiscal year, preserving Offset.

public static DateTimeOffset StartOfFiscalYear(this DateTimeOffset dateTime, int firstMonthOfFiscalYear = 1)

Parameters

dateTime DateTimeOffset
firstMonthOfFiscalYear int

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

Returns

DateTimeOffset

StartOfMonth(DateTimeOffset)

Returns a new DateTimeOffset set to 00:00:00 on the 1st day of the month, preserving Offset.

public static DateTimeOffset StartOfMonth(this DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset

Returns

DateTimeOffset

StartOfQuarter(DateTimeOffset)

Returns a new DateTimeOffset set to 00:00:00 on the 1st day of the quarter (Q1=Jan, Q2=Apr, Q3=Jul, Q4=Oct), preserving Offset.

public static DateTimeOffset StartOfQuarter(this DateTimeOffset dateTime)

Parameters

dateTime DateTimeOffset

Returns

DateTimeOffset

StartOfWeek(DateTimeOffset, DayOfWeek)

Returns a new DateTimeOffset set to 00:00:00 on the first day of the week, preserving Offset.

public static DateTimeOffset StartOfWeek(this DateTimeOffset dateTime, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)

Parameters

dateTime DateTimeOffset
firstDayOfWeek DayOfWeek

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

Returns

DateTimeOffset

ToTimeZoneSilently(DateTimeOffset, string)

Converts the given DateTimeOffset to the specified time zone. Returns null if the time zone ID is invalid or not found.

public static DateTimeOffset? ToTimeZoneSilently(this DateTimeOffset dateTime, string tzId)

Parameters

dateTime DateTimeOffset
tzId string

Returns

DateTimeOffset?