Class DateTimeExtensions
Provides extension methods for working with DateTime values.
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public static class DateTimeExtensions
Methods
BeginningOfTheDay(DateTime)
Gets the beginning of the day for a given DateTime.
Declaration
public static DateTime BeginningOfTheDay(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
Returns
| Type | Description |
|---|---|
| DateTime | The beginning of the day. |
Ceil(DateTime, TimeSpan)
Declaration
public static DateTime Ceil(this DateTime date, TimeSpan span)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
| TimeSpan | span | The TimeSpan to ceil to. |
Returns
| Type | Description |
|---|---|
| DateTime | The ceiled DateTime value. |
EndOfMonth(DateTime)
Gets the last day and time of the month for a given DateTime.
Declaration
public static DateTime EndOfMonth(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime value. |
Returns
| Type | Description |
|---|---|
| DateTime | The last day of the month. |
EndOfTheDay(DateTime)
Gets the end of the day for a given DateTime.
Declaration
public static DateTime EndOfTheDay(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
Returns
| Type | Description |
|---|---|
| DateTime | The end of the day. |
EndOfWeek(DateTime)
Gets the last day of the week for a given DateTime.
Declaration
public static DateTime EndOfWeek(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime value. |
Returns
| Type | Description |
|---|---|
| DateTime | The last day of the week. |
FillInEmptyDates<T>(IEnumerable<DateTime>, IEnumerable<T>, Func<T, DateTime>, Func<DateTime, T>)
Fills in missing dates in a sequence of data using a default item factory.
Declaration
public static IEnumerable<T> FillInEmptyDates<T>(this IEnumerable<DateTime> allDates, IEnumerable<T> sourceData, Func<T, DateTime> dateSelector, Func<DateTime, T> defaultItemFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<DateTime> | allDates | The list of all dates. |
| IEnumerable<T> | sourceData | The source data collection. |
| Func<T, DateTime> | dateSelector | A function to extract the date from a data item. |
| Func<DateTime, T> | defaultItemFactory | A function to create a default item based on a date. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An IEnumerable<T> with missing dates filled in. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the data items. |
Floor(DateTime, TimeSpan)
Declaration
public static DateTime Floor(this DateTime date, TimeSpan span)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
| TimeSpan | span | The TimeSpan to floor to. |
Returns
| Type | Description |
|---|---|
| DateTime | The floored DateTime value. |
GetAge(DateTime)
Gets the age based on the difference between a birthdate and the current date.
Declaration
public static int GetAge(this DateTime birthDate)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | birthDate | The birthdate. |
Returns
| Type | Description |
|---|---|
| int | The calculated age. |
GetAge(DateTime, DateTime)
Gets the age of a person based on their birthdate and a given reference date.
Declaration
public static int GetAge(this DateTime birthDate, DateTime at)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | birthDate | The birthdate of the person. |
| DateTime | at |
Returns
| Type | Description |
|---|---|
| int | The age of the person. |
GetDateRange(DateTime, DateTime)
Generates a range of dates between two DateTime values.
Declaration
public static IEnumerable<DateTime> GetDateRange(this DateTime startDate, DateTime endDate)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | startDate | The start date. |
| DateTime | endDate | The end date. |
Returns
| Type | Description |
|---|---|
| IEnumerable<DateTime> | An IEnumerable<T> of DateTime values within the range. |
GetDaySuffix(DateTime)
Gets the suffix for the day in a DateTime (e.g., "st", "nd", "rd", "th").
Declaration
public static string GetDaySuffix(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
Returns
| Type | Description |
|---|---|
| string | The day suffix. |
LastDayOfMonth(DateTime)
Gets the last day of the month for a given DateTime.
Declaration
public static DateTime LastDayOfMonth(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime value. |
Returns
| Type | Description |
|---|---|
| DateTime | The last day of the month. |
Round(DateTime, TimeSpan)
Declaration
public static DateTime Round(this DateTime date, TimeSpan span)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
| TimeSpan | span | The TimeSpan to round to. |
Returns
| Type | Description |
|---|---|
| DateTime | The rounded DateTime value. |
StartOfMonth(DateTime)
Gets the first day of the month for a given DateTime.
Declaration
public static DateTime StartOfMonth(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime value. |
Returns
| Type | Description |
|---|---|
| DateTime | The first day of the month. |
StartOfWeek(DateTime, DayOfWeek)
Gets the start of the week for a given DateTime.
Declaration
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime value. |
| DayOfWeek | startOfWeek | The starting day of the week. |
Returns
| Type | Description |
|---|---|
| DateTime | The start of the week DateTime value. |
TimeAgo(DateTime)
Converts a DateTime to a human-readable time ago format.
Declaration
public static string TimeAgo(this DateTime dateTime)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dateTime | The DateTime value. |
Returns
| Type | Description |
|---|---|
| string | The time ago representation. |
ToNegative(TimeSpan)
Declaration
public static TimeSpan ToNegative(this TimeSpan span)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | span |
Returns
| Type | Description |
|---|---|
| TimeSpan |
ToPositive(TimeSpan)
Converts a TimeSpan to a positive value if it is not already positive.
Declaration
public static TimeSpan ToPositive(this TimeSpan span)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | span |
Returns
| Type | Description |
|---|---|
| TimeSpan | an positive TimeSpan |
Truncate(DateTime, long)
Truncates a DateTime to a specified resolution.
Declaration
public static DateTime Truncate(this DateTime date, long resolution)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | The DateTime value. |
| long | resolution | The resolution to truncate to. |
Returns
| Type | Description |
|---|---|
| DateTime | The truncated DateTime value. |