Table of Contents

Class DateTimeExtensions

Namespace
DotNetExtensionKit
Assembly
DotNetExtensionKit.dll

Provides extension methods for DateTime manipulation including day, week, month, and year boundaries, age calculation, and date utility checks.

public static class DateTimeExtensions
Inheritance
DateTimeExtensions
Inherited Members

Methods

Age(DateTime)

Calculates the age in whole years from the birth date to today.

public static int Age(this DateTime birthDate)

Parameters

birthDate DateTime

The date of birth.

Returns

int

The age in whole years as of today's date.

Examples

DateTime dob = new DateTime(1990, 3, 25);
int age = dob.Age();
// age == number of whole years between dob and DateTime.Today

Exceptions

ArgumentOutOfRangeException

Thrown when the birth date is after today.

Age(DateTime, DateTime)

Calculates the age in whole years from the birth date to the specified reference date. Subtracts one year if the birthday has not yet occurred in the reference year.

public static int Age(this DateTime birthDate, DateTime asOf)

Parameters

birthDate DateTime

The date of birth.

asOf DateTime

The reference date to calculate age against.

Returns

int

The age in whole years as of the specified reference date.

Examples

DateTime dob = new DateTime(1990, 3, 25);
int age = dob.Age(new DateTime(2024, 6, 15));
// age == 34

Exceptions

ArgumentOutOfRangeException

Thrown when the birth date is after the reference date.

EndOfDay(DateTime)

Returns a DateTime representing the end of the day (23:59:59.9999999) for the given date, preserving the original DateTimeKind.

public static DateTime EndOfDay(this DateTime dateTime)

Parameters

dateTime DateTime

The date for which to calculate the end of the day.

Returns

DateTime

A new DateTime set to the last tick of the day (23:59:59.9999999) on the same date, with the original Kind preserved.

Examples

DateTime now = new DateTime(2024, 6, 15, 14, 30, 0);
DateTime end = now.EndOfDay();
// end == 2024-06-15 23:59:59.9999999

EndOfMonth(DateTime)

Returns a DateTime representing the last day of the month at 23:59:59.9999999, preserving the original DateTimeKind.

public static DateTime EndOfMonth(this DateTime dateTime)

Parameters

dateTime DateTime

The date for which to calculate the end of the month.

Returns

DateTime

A new DateTime set to the last day of the month at the last tick of the day, with the original Kind preserved.

Examples

DateTime date = new DateTime(2024, 6, 15);
DateTime monthEnd = date.EndOfMonth();
// monthEnd == 2024-06-30 23:59:59.9999999

EndOfWeek(DateTime, DayOfWeek)

Returns a DateTime representing the end of the week (23:59:59.9999999) for the given date, six days after the start of the week. Preserves the original DateTimeKind.

public static DateTime EndOfWeek(this DateTime dateTime, DayOfWeek startOfWeek = DayOfWeek.Monday)

Parameters

dateTime DateTime

The date for which to calculate the end of the week.

startOfWeek DayOfWeek

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

Returns

DateTime

A new DateTime set to the last tick of the day, six days after the start of the week, with the original Kind preserved.

Examples

DateTime wednesday = new DateTime(2024, 6, 12); // Wednesday
DateTime weekEnd = wednesday.EndOfWeek();
// weekEnd == 2024-06-16 (Sunday) 23:59:59.9999999

EndOfYear(DateTime)

Returns a DateTime representing December 31st of the year at 23:59:59.9999999, preserving the original DateTimeKind.

public static DateTime EndOfYear(this DateTime dateTime)

Parameters

dateTime DateTime

The date for which to calculate the end of the year.

Returns

DateTime

A new DateTime set to December 31st at the last tick of the day, with the original Kind preserved.

Examples

DateTime date = new DateTime(2024, 6, 15);
DateTime yearEnd = date.EndOfYear();
// yearEnd == 2024-12-31 23:59:59.9999999

IsBetween(DateTime, DateTime, DateTime)

Returns true if the DateTime falls within the inclusive range [start, end].

public static bool IsBetween(this DateTime dateTime, DateTime start, DateTime end)

Parameters

dateTime DateTime

The date to check.

start DateTime

The inclusive start of the range.

end DateTime

The inclusive end of the range.

Returns

bool

true if dateTime is greater than or equal to start and less than or equal to end; otherwise, false.

Examples

DateTime date = new DateTime(2024, 6, 15);
bool inRange = date.IsBetween(new DateTime(2024, 6, 1), new DateTime(2024, 6, 30));
// inRange == true

IsWeekday(DateTime)

Returns true if the DateTime falls on a weekday (Monday through Friday).

public static bool IsWeekday(this DateTime dateTime)

Parameters

dateTime DateTime

The date to check.

Returns

bool

true if the date is Monday through Friday; otherwise, false.

Examples

DateTime monday = new DateTime(2024, 6, 10); // Monday
bool result = monday.IsWeekday();
// result == true

IsWeekend(DateTime)

Returns true if the DateTime falls on a Saturday or Sunday.

public static bool IsWeekend(this DateTime dateTime)

Parameters

dateTime DateTime

The date to check.

Returns

bool

true if the date is a Saturday or Sunday; otherwise, false.

Examples

DateTime saturday = new DateTime(2024, 6, 15); // Saturday
bool result = saturday.IsWeekend();
// result == true

StartOfDay(DateTime)

Returns a DateTime representing the start of the day (00:00:00.000) for the given date, preserving the original DateTimeKind.

public static DateTime StartOfDay(this DateTime dateTime)

Parameters

dateTime DateTime

The date for which to calculate the start of the day.

Returns

DateTime

A new DateTime set to midnight (00:00:00.000) on the same date, with the original Kind preserved.

Examples

DateTime now = new DateTime(2024, 6, 15, 14, 30, 0);
DateTime start = now.StartOfDay();
// start == 2024-06-15 00:00:00.000

StartOfMonth(DateTime)

Returns a DateTime representing the first day of the month at 00:00:00.000, preserving the original DateTimeKind.

public static DateTime StartOfMonth(this DateTime dateTime)

Parameters

dateTime DateTime

The date for which to calculate the start of the month.

Returns

DateTime

A new DateTime set to the first day of the month at midnight, with the original Kind preserved.

Examples

DateTime date = new DateTime(2024, 6, 15);
DateTime monthStart = date.StartOfMonth();
// monthStart == 2024-06-01 00:00:00.000

StartOfWeek(DateTime, DayOfWeek)

Returns a DateTime representing the start of the week (00:00:00.000) for the given date, finding the most recent occurrence of the specified day of the week. Preserves the original DateTimeKind.

public static DateTime StartOfWeek(this DateTime dateTime, DayOfWeek startOfWeek = DayOfWeek.Monday)

Parameters

dateTime DateTime

The date for which to calculate the start of the week.

startOfWeek DayOfWeek

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

Returns

DateTime

A new DateTime set to midnight on the most recent occurrence of startOfWeek, with the original Kind preserved.

Examples

DateTime wednesday = new DateTime(2024, 6, 12); // Wednesday
DateTime weekStart = wednesday.StartOfWeek();
// weekStart == 2024-06-10 (Monday) 00:00:00.000

DateTime sundayStart = wednesday.StartOfWeek(DayOfWeek.Sunday);
// sundayStart == 2024-06-09 (Sunday) 00:00:00.000

StartOfYear(DateTime)

Returns a DateTime representing January 1st of the year at 00:00:00.000, preserving the original DateTimeKind.

public static DateTime StartOfYear(this DateTime dateTime)

Parameters

dateTime DateTime

The date for which to calculate the start of the year.

Returns

DateTime

A new DateTime set to January 1st at midnight, with the original Kind preserved.

Examples

DateTime date = new DateTime(2024, 6, 15);
DateTime yearStart = date.StartOfYear();
// yearStart == 2024-01-01 00:00:00.000