Class DateTimeExtensions
DateTime extension methods
Inherited Members
Namespace: PayrollEngine.Client.Scripting
Assembly: PayrollEngine.Client.Scripting.dll
Syntax
public static class DateTimeExtensions
Methods
View SourceAge(DateTime)
Calculates the current age, counting the completed years
Declaration
public static int Age(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date |
Returns
| Type | Description |
|---|---|
| int | The current age |
Age(DateTime, DateTime)
Calculates the age at a specific moment, counting the completed years
Declaration
public static int Age(this DateTime date, DateTime testMoment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DateTime | testMoment | The test moment |
Returns
| Type | Description |
|---|---|
| int | The age at the test moment |
DayEnd(DateTime)
Get the day end date and time in UTC
Declaration
public static DateTime DayEnd(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | Last moment of the day |
DayStart(DateTime)
Get the day start date and time in UTC
Declaration
public static DateTime DayStart(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | First moment of the day |
DaysInMonth(DateTime)
Returns the number of days in the month of a specific date
Declaration
public static int DaysInMonth(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| int | The number of days in for the specified moment /> |
FirstDayOfWeek(int, int)
Get week start date by ISO 8601 week number of the year
Declaration
public static DateTime FirstDayOfWeek(int year, int weekOfYear)
Parameters
| Type | Name | Description |
|---|---|---|
| int | year | The year |
| int | weekOfYear | The ISO 8601 week number of the year |
Returns
| Type | Description |
|---|---|
| DateTime | First day of the week |
FirstDayOfWeek(int, int, CultureInfo)
Get week start date by ISO 8601 week number of the year
Declaration
public static DateTime FirstDayOfWeek(int year, int weekOfYear, CultureInfo culture)
Parameters
| Type | Name | Description |
|---|---|---|
| int | year | The year |
| int | weekOfYear | The ISO 8601 week number of the year |
| CultureInfo | culture | The calendar culture |
Returns
| Type | Description |
|---|---|
| DateTime | First day of the week |
GetIso8601WeekOfYear(DateTime)
Get ISO 8601 week number of the year
Declaration
public static int GetIso8601WeekOfYear(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| int | ISO 8601 week number |
GetIso8601WeekOfYear(DateTime, CultureInfo)
Get ISO 8601 week number of the year
Declaration
public static int GetIso8601WeekOfYear(this DateTime moment, CultureInfo culture)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| CultureInfo | culture | The calendar culture |
Returns
| Type | Description |
|---|---|
| int | ISO 8601 week number |
GetNextWeekDay(DateTime, DayOfWeek)
Get the next matching day
Declaration
public static DateTime GetNextWeekDay(this DateTime date, DayOfWeek dayOfWeek)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DayOfWeek | dayOfWeek | Target day of week |
Returns
| Type | Description |
|---|---|
| DateTime | The next matching day |
GetPreviousWeekDay(DateTime, DayOfWeek)
Get the previous matching day
Declaration
public static DateTime GetPreviousWeekDay(this DateTime date, DayOfWeek dayOfWeek)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DayOfWeek | dayOfWeek | Target day of week |
Returns
| Type | Description |
|---|---|
| DateTime | The previous matching day |
IsAfter(DateTime, DatePeriod)
Test if a specific time moment is after a period
Declaration
public static bool IsAfter(this DateTime moment, DatePeriod period)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| DatePeriod | period | The period to test |
Returns
| Type | Description |
|---|---|
| bool | True, if the moment is after the period |
IsBefore(DateTime, DatePeriod)
Test if a specific time moment is before a period
Declaration
public static bool IsBefore(this DateTime moment, DatePeriod period)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| DatePeriod | period | The period to test |
Returns
| Type | Description |
|---|---|
| bool | True, if the moment is before the period |
IsDayOfWeek(DateTime, IEnumerable<DayOfWeek>)
Test for working day
Declaration
public static bool IsDayOfWeek(this DateTime date, IEnumerable<DayOfWeek> days)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| IEnumerable<DayOfWeek> | days | Available days |
Returns
| Type | Description |
|---|---|
| bool | True if date is a working day |
IsFirstDay(DateTime, DatePeriod)
Test if a specific time moment is the first day of a period
Declaration
public static bool IsFirstDay(this DateTime moment, DatePeriod period)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| DatePeriod | period | The period |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the first period day |
IsFirstDayOfMonth(DateTime)
Test if a specific day is the first day of the month
Declaration
public static bool IsFirstDayOfMonth(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the first day of the month |
IsFirstDayOfMonth(DateTime, Month)
Test if a specific day is the first day of the month
Declaration
public static bool IsFirstDayOfMonth(this DateTime moment, Month month)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| Month | month | The month to test |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the first day of the month |
IsFirstDayOfYear(DateTime)
Test if a specific day is the first day of the year
Declaration
public static bool IsFirstDayOfYear(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the first day in the year |
IsLastDay(DateTime, DatePeriod)
Test if a specific time moment is the last day of a period
Declaration
public static bool IsLastDay(this DateTime moment, DatePeriod period)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| DatePeriod | period | The period |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the last period day |
IsLastDayOfMonth(DateTime, Month, bool)
Test if a specific day is the last day of the month
Declaration
public static bool IsLastDayOfMonth(this DateTime moment, Month month, bool ignoreLeapYear = false)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| Month | month | The month to test |
| bool | ignoreLeapYear | Ignore the leap year day |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the last day of the month |
IsLastDayOfMonth(DateTime, bool)
Test if a specific day is the last day of the month
Declaration
public static bool IsLastDayOfMonth(this DateTime moment, bool ignoreLeapYear = false)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| bool | ignoreLeapYear | Ignore the leap year day |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the last day of the month |
IsLastDayOfYear(DateTime)
Test if a specific time moment is the last day of the year
Declaration
public static bool IsLastDayOfYear(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| bool | True if the test day is the last day in the year |
IsLastMomentOfDay(DateTime)
Test if the date is the last moment of the day
Declaration
public static bool IsLastMomentOfDay(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date |
Returns
| Type | Description |
|---|---|
| bool | True on the last moment of the day |
IsMidnight(DateTime)
Test if the date is midnight
Declaration
public static bool IsMidnight(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date |
Returns
| Type | Description |
|---|---|
| bool | True in case the moment is date |
IsSameDay(DateTime, DateTime)
Test if two dates are in the same day
Declaration
public static bool IsSameDay(this DateTime date, DateTime compare)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DateTime | compare | The second date to test |
Returns
| Type | Description |
|---|---|
| bool | Return true if both dates are in the same day |
IsSameHour(DateTime, DateTime)
Test if two dates are in the same hour
Declaration
public static bool IsSameHour(this DateTime date, DateTime compare)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DateTime | compare | The second date to test |
Returns
| Type | Description |
|---|---|
| bool | Return true if both dates are in the same hour |
IsSameMonth(DateTime, DateTime)
Test if two dates are in the same year and month
Declaration
public static bool IsSameMonth(this DateTime date, DateTime compare)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DateTime | compare | The second date to test |
Returns
| Type | Description |
|---|---|
| bool | Return true if year and mont of both dates is equal |
IsSameYear(DateTime, DateTime)
Test if two dates are in the same year
Declaration
public static bool IsSameYear(this DateTime date, DateTime compare)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| DateTime | compare | The second date to test |
Returns
| Type | Description |
|---|---|
| bool | Return true if year and mont of both dates is equal |
IsUtc(DateTime)
Test if the date is in UTC
Declaration
public static bool IsUtc(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| bool | True, date time is UTC |
IsWithin(DateTime, DatePeriod)
Test if a specific time moment is within a period
Declaration
public static bool IsWithin(this DateTime moment, DatePeriod period)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| DatePeriod | period | The period |
Returns
| Type | Description |
|---|---|
| bool | True if the test is within the period |
IsWithin(DateTime, DateTime, DateTime)
Test if a specific time moment is within a period
Declaration
public static bool IsWithin(this DateTime moment, DateTime start, DateTime end)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| DateTime | start | The period start |
| DateTime | end | The period end |
Returns
| Type | Description |
|---|---|
| bool | True if the test is within start and end |
LastDayOfWeek(int, int)
Get weekend date by ISO 8601 week number of the year
Declaration
public static DateTime LastDayOfWeek(int year, int weekOfYear)
Parameters
| Type | Name | Description |
|---|---|---|
| int | year | The year |
| int | weekOfYear | The ISO 8601 week number of the year |
Returns
| Type | Description |
|---|---|
| DateTime | Last day of the week |
LastDayOfWeek(int, int, CultureInfo)
Get weekend date by ISO 8601 week number of the year
Declaration
public static DateTime LastDayOfWeek(int year, int weekOfYear, CultureInfo culture)
Parameters
| Type | Name | Description |
|---|---|---|
| int | year | The year |
| int | weekOfYear | The ISO 8601 week number of the year |
| CultureInfo | culture | The calendar culture |
Returns
| Type | Description |
|---|---|
| DateTime | Last day of the week |
LastMomentOfDay(DateTime)
Return the last moment of the day
Declaration
public static DateTime LastMomentOfDay(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date |
Returns
| Type | Description |
|---|---|
| DateTime | DateTime from the latest moment in a day |
Month(DateTime)
Returns the date month as enumeration
Declaration
public static Month Month(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| Month | The date month |
MonthEnd(DateTime)
Get the month end date in UTC
Declaration
public static DateTime MonthEnd(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | Last moment of the month |
MonthStart(DateTime)
Get the month start date in UTC
Declaration
public static DateTime MonthStart(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | First moment of the month |
NextTick(DateTime)
Get the next tick
Declaration
public static DateTime NextTick(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date |
Returns
| Type | Description |
|---|---|
| DateTime | The next tick |
PreviousTick(DateTime)
Get the previous tick
Declaration
public static DateTime PreviousTick(this DateTime date)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date |
Returns
| Type | Description |
|---|---|
| DateTime | The previous tick |
Round(DateTime, TimeSpan)
Rounds a date time to the nearest value
Declaration
public static DateTime Round(this DateTime date, TimeSpan stepSize)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| TimeSpan | stepSize | Size of the rounding step |
Returns
| Type | Description |
|---|---|
| DateTime | The rounded date time |
RoundDown(DateTime, TimeSpan)
Rounds a date time down
Declaration
public static DateTime RoundDown(this DateTime date, TimeSpan stepSize)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| TimeSpan | stepSize | Size of the rounding step |
Returns
| Type | Description |
|---|---|
| DateTime | The rounded date time |
RoundTickToHour(DateTime)
Round to hour, if it is the last tick from an hour
Declaration
public static DateTime RoundTickToHour(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | Date of the next hour if the input is on the last tick on an hour, else the original value |
RoundUp(DateTime, TimeSpan)
Rounds a date time up
Declaration
public static DateTime RoundUp(this DateTime date, TimeSpan stepSize)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | date | |
| TimeSpan | stepSize | Size of the rounding step |
Returns
| Type | Description |
|---|---|
| DateTime | The rounded date time |
Subtract(DateTime, TimeSpan)
Returns the DateTime resulting from subtracting a time span to this DateTime
Declaration
public static DateTime Subtract(this DateTime moment, TimeSpan timeSpan)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| TimeSpan | timeSpan | The time span to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractDays(DateTime, double)
Returns the DateTime resulting from subtracting a fractional number of days to this DateTime
Declaration
public static DateTime SubtractDays(this DateTime moment, double days)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| double | days | The days to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractHours(DateTime, double)
Returns the DateTime resulting from subtracting a fractional number of hours to this DateTime
Declaration
public static DateTime SubtractHours(this DateTime moment, double hours)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| double | hours | The hours to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractMinutes(DateTime, double)
Returns the DateTime resulting from subtracting a fractional number of minutes to this DateTime
Declaration
public static DateTime SubtractMinutes(this DateTime moment, double minutes)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| double | minutes | The minutes to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractMonths(DateTime, int)
Returns the DateTime resulting from subtracting a number of months to this DateTime
Declaration
public static DateTime SubtractMonths(this DateTime moment, int months)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| int | months | The months to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractSeconds(DateTime, double)
Returns the DateTime resulting from subtracting a fractional number of seconds to this DateTime
Declaration
public static DateTime SubtractSeconds(this DateTime moment, double seconds)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| double | seconds | The seconds to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractTicks(DateTime, long)
Returns the DateTime resulting from subtracting a number of ticks to this DateTime
Declaration
public static DateTime SubtractTicks(this DateTime moment, long ticks)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| long | ticks | The ticks to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
SubtractYears(DateTime, int)
Returns the DateTime resulting from subtracting a number of years to this DateTime
Declaration
public static DateTime SubtractYears(this DateTime moment, int years)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| int | years | The years to subtract |
Returns
| Type | Description |
|---|---|
| DateTime |
ToCompactString(DateTime)
Format a compact date (removes empty time parts)
Declaration
public static string ToCompactString(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| string | The formatted period start date |
ToPeriodEndString(DateTime)
Format a period end date (removed empty time parts, and round last moment values), using the current culture
Declaration
public static string ToPeriodEndString(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| string | The formatted period end date |
ToPeriodStartString(DateTime)
Format a period start date (removes empty time parts), using the current culture
Declaration
public static string ToPeriodStartString(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| string | The formatted period start date |
ToUtc(DateTime)
Convert a date into the UTC value. Dates (without time part) are used without time adaption
Declaration
public static DateTime ToUtc(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | The UTC date time |
ToUtcString(DateTime)
Convert a date into the UTC string value, using the current culture
Declaration
public static string ToUtcString(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| string | The UTC date time string |
ToUtcString(DateTime, IFormatProvider)
Convert a date into the UTC string value
Declaration
public static string ToUtcString(this DateTime moment, IFormatProvider provider)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment | |
| IFormatProvider | provider | The format provider |
Returns
| Type | Description |
|---|---|
| string | The UTC date time string |
ToUtcTime(DateTime)
Convert a date into the UTC valueDates (without time part) are used without time adaption
Declaration
public static DateTime ToUtcTime(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | The UTC date time |
YearEnd(DateTime)
Get the year-end date in UTC
Declaration
public static DateTime YearEnd(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | Last moment of the year |
YearStart(DateTime)
Get the year start date in UTC
Declaration
public static DateTime YearStart(this DateTime moment)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | moment |
Returns
| Type | Description |
|---|---|
| DateTime | First moment of the year |