Class DateTimeHandling
- Namespace
- ArcanaDevelopment.adTempus.Shared
- Assembly
- ArcanaDevelopment.adTempus.Shared.dll
Utilities for working with dates and times
public static class DateTimeHandling
- Inheritance
-
objectDateTimeHandling
Methods
Abs(TimeSpan)
public static TimeSpan Abs(TimeSpan value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
TimeSpan |
Returns
- TimeSpan
CalculateEaster(int)
Calculates the date of Easter for a given year.
public static DateTime CalculateEaster(int year)
Parameters
| Name | Type | Description |
|---|---|---|
year |
int |
Returns
- DateTime
Remarks
Easter calculation from http://www.assa.org.au/edm.html#Computer by Ronald W. Mallen (Astronomical Society of South Australia)
Combine(DateTime, DateTime)
Combines two DateTime values, using the date from one value and the time from the other.
public static DateTime Combine(DateTime datePortion, DateTime timePortion)
Parameters
| Name | Type | Description |
|---|---|---|
datePortion |
DateTime | |
timePortion |
DateTime |
Returns
- DateTime
A new DateTime value with the date set to the date from the datePortion, the hours and minutes set from the time portion, and seconds and milliseconds set to 0.
CompareDateTime(DateTimeOffset?, DateTimeOffset?)
Compares two nullable DateTimeOffset values
public static int CompareDateTime(DateTimeOffset? x, DateTimeOffset? y)
Parameters
| Name | Type | Description |
|---|---|---|
x |
DateTimeOffset? | |
y |
DateTimeOffset? |
Returns
- int
The comparison result
Remarks
This method treats a null as less than a non-null value. If both x and y have values, it returns
the result of System.DateTimeOffset.Compare(System.DateTimeOffset, System.DateTimeOffset)(x, y).
CompareDateTime(DateTime?, DateTime?)
Compares two nullable DateTime values
public static int CompareDateTime(DateTime? x, DateTime? y)
Parameters
| Name | Type | Description |
|---|---|---|
x |
DateTime? | |
y |
DateTime? |
Returns
- int
The comparison result
Remarks
This method treats a null as less than a non-null value. If both x and y have values, it returns
the result of System.DateTime.Compare(System.DateTime, System.DateTime)(x, y).
CompareTime(DateTime, DateTime)
Compares only the time portion of two DateTime values (hour and minute only; milliseconds are ignored).
public static int CompareTime(DateTime value1, DateTime value2)
Parameters
| Name | Type | Description |
|---|---|---|
value1 |
DateTime | |
value2 |
DateTime |
Returns
- int
CompareTimeSpan(TimeSpan?, TimeSpan?)
Compares two nullable TimeSpan values
public static int CompareTimeSpan(TimeSpan? x, TimeSpan? y)
Parameters
| Name | Type | Description |
|---|---|---|
x |
TimeSpan? | |
y |
TimeSpan? |
Returns
- int
The comparison result
Remarks
This method treats a null as less than a non-null value. If both x and y have values, it returns
the result of System.DateTime.Compare(System.DateTime, System.DateTime)(x, y).
ConvertTimeZone(DateTime, TimeSpan)
Converts a DateTime from one time zone to another
public static DateTime ConvertTimeZone(DateTime src, TimeSpan targetTZBias)
Parameters
| Name | Type | Description |
|---|---|---|
src |
DateTime | The date/time to convert (assumed to be in the local time zone) |
targetTZBias |
TimeSpan | The time zone bias to convert to |
Returns
- DateTime
Remarks
This method assumes that src is in the current local time zone and uses CurrentTZBias() to get that bias.
ConvertTimeZone(DateTime, TimeSpan, TimeSpan)
Converts a DateTime from one time zone to another
public static DateTime ConvertTimeZone(DateTime src, TimeSpan srcTZBias, TimeSpan targetTZBias)
Parameters
| Name | Type | Description |
|---|---|---|
src |
DateTime | The date/time to convert |
srcTZBias |
TimeSpan | The time zone bias of the |
targetTZBias |
TimeSpan | The time zone bias to convert to |
Returns
- DateTime
CurrentTZBias()
Gets the current time zone bias (offset from UTC)
public static TimeSpan CurrentTZBias()
Returns
- TimeSpan
The current bias, adjusted for Daylight Saving Time if necessary
DayOfWeek(DateTime)
Returns an integer corresponding to the day of the week for the specified date, with Sunday=1, Monday=2, ..., Saturday=7
public static Weekday DayOfWeek(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
Remarks
This method converts from the DayOfWeek value returned by DateTime.DayOfWeek, which uses Sunday=0. adTempus uses Sunday=1.
DescribeDateRange(DateTime?, DateTime?)
Provides a readable description of a date range
public static string DescribeDateRange(DateTime? startDate, DateTime? endDate)
Parameters
| Name | Type | Description |
|---|---|---|
startDate |
DateTime? | |
endDate |
DateTime? |
Returns
- string
EndOfDay(DateTime)
Returns a DateTime value representing 11:59:59 PM on the source day.
public static DateTime EndOfDay(DateTime src)
Parameters
| Name | Type | Description |
|---|---|---|
src |
DateTime |
Returns
- DateTime
FirstDayOfMonth(DateTime)
Returns a DateTime value representing the first day of the month that includes the source date.
public static DateTime FirstDayOfMonth(DateTime date)
Parameters
| Name | Type | Description |
|---|---|---|
date |
DateTime |
Returns
- DateTime
FirstDayOfYear(DateTime)
Gets a DateTime representing January 1 of the year of a specified date.
public static DateTime FirstDayOfYear(DateTime date)
Parameters
| Name | Type | Description |
|---|---|---|
date |
DateTime |
Returns
- DateTime
FirstWeekdayOfMonth(DateTime)
Returns an integer indicating what day is the first day of the month in which the specified date occurs (Sunday=1...Saturday=7).
public static Weekday FirstWeekdayOfMonth(DateTime date)
Parameters
| Name | Type | Description |
|---|---|---|
date |
DateTime |
Returns
FormatSeconds(int)
Formats a number of seconds into a more readable format.
public static string FormatSeconds(int numberOfSeconds)
Parameters
| Name | Type | Description |
|---|---|---|
numberOfSeconds |
int |
Returns
- string
Remarks
FormatTimespan(TimeSpan?)
Formats a timespan into a readable format.
public static string FormatTimespan(TimeSpan? ts)
Parameters
| Name | Type | Description |
|---|---|---|
ts |
TimeSpan? | The timespan to format |
Returns
- string
Remarks
If ts is less than 1 minute and ts.Milliseconds=0, returns "0:ss".
If ts is less than 1 minute and ts.Milliseconds!=0, returns "0:ss.iii".
If ts is less than 1 hour, returns minutes and seconds "m:ss".
If ts is greater than 1 hour, returns hours, minutes, and seconds "h:mm:ss".
If ts is greater than 24 hours, returns days, hours, minutes, and seconds "d:h:mm:ss".
IsEndOfDay(DateTime)
Determines whether the time portion of a DateTime is 11:59pm (seconds ignored)
public static bool IsEndOfDay(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- bool
IsLastDayOfMonth(DateTime)
Determines whether a date value is the last day of the month.
public static bool IsLastDayOfMonth(DateTime val)
Parameters
| Name | Type | Description |
|---|---|---|
val |
DateTime |
Returns
- bool
IsLastOccurrenceInMonth(DateTime)
Indicates whether a date is the last occurrence of a given weekday in the month. For example, if the value is a Monday, this method returns True if the date is the last Monday of the month, or False if there are other Mondays after it.
public static bool IsLastOccurrenceInMonth(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- bool
IsStartOfDay(DateTime)
Determines whether the time portion of a DateTime is 12:00am (seconds ignored)
public static bool IsStartOfDay(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- bool
LastDayOfMonth(DateTime)
Returns a DateTime representing the last day of the month in which the specified date occurs.
public static DateTime LastDayOfMonth(DateTime date)
Parameters
| Name | Type | Description |
|---|---|---|
date |
DateTime |
Returns
- DateTime
LastDayOfMonth(int, int)
Returns a DateTime representing the last day of the specified month.
public static DateTime LastDayOfMonth(int year, int month)
Parameters
| Name | Type | Description |
|---|---|---|
year |
int | |
month |
int |
Returns
- DateTime
NextMinute(DateTime)
Gets a DateTime representing the start of the next minute
public static DateTime NextMinute(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- DateTime
NextMinute(DateTimeOffset)
Gets a DateTime representing the start of the next minute
public static DateTimeOffset NextMinute(DateTimeOffset value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTimeOffset |
Returns
- DateTimeOffset
OccurrenceInMonth(DateTime)
Indicates what occurrence of a given day of the week the specified date represents. For example, if the value is a Monday, this method determines whether it is the first, second, third, fourth, or fifth Monday of the month.
public static int OccurrenceInMonth(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- int
RemoveSeconds(DateTime)
Returns a new DateTime value identical to the source but with seconds and milliseconds set to 0.
public static DateTime RemoveSeconds(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- DateTime
RemoveSeconds(DateTimeOffset)
Returns a new DateTimeOffset value identical to the source but with seconds and milliseconds set to 0.
public static DateTimeOffset RemoveSeconds(DateTimeOffset value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTimeOffset |
Returns
- DateTimeOffset
RepresentsFullYear(DateTime?, DateTime?)
Indicates whether the specified pair of dates is January 1 to December 31 of the same year.
public static bool RepresentsFullYear(DateTime? StartDate, DateTime? EndDate)
Parameters
| Name | Type | Description |
|---|---|---|
StartDate |
DateTime? | |
EndDate |
DateTime? |
Returns
- bool
ShortDateTime(DateTime?)
Returns the date and time in the short format for the locale
public static string ShortDateTime(DateTime? val)
Parameters
| Name | Type | Description |
|---|---|---|
val |
DateTime? |
Returns
- string
StartOfDay(DateTime)
Returns a DateTime value representing 12:00:00 AM on the source day.
public static DateTime StartOfDay(DateTime src)
Parameters
| Name | Type | Description |
|---|---|---|
src |
DateTime |
Returns
- DateTime
TimeString(DateTime)
Returns the time portion of the specified value, in short time format.
public static string TimeString(DateTime source)
Parameters
| Name | Type | Description |
|---|---|---|
source |
DateTime |
Returns
- string
TimeValue(DateTime)
Returns a DateTime containing only the time portion from the source value. The date portion is set to January 1,1900.
public static DateTime TimeValue(DateTime src)
Parameters
| Name | Type | Description |
|---|---|---|
src |
DateTime |
Returns
- DateTime
TimeValue(int, int)
Returns a DateTime representing the time portion only for the specified hour and minute
public static DateTime TimeValue(int hour, int minute)
Parameters
| Name | Type | Description |
|---|---|---|
hour |
int | |
minute |
int |
Returns
- DateTime
Remarks
The Date portion of the value is set to January 1, 1900. Seconds and milliseconds are set to 0.
TimeValue(DateTime?)
Returns a DateTime containing only the time portion from the source value. The date portion is set to January 1,1900.
public static DateTime? TimeValue(DateTime? value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime? |
Returns
WeekOfMonth(DateTime)
Indicates what week of the month a value falls in.
public static int WeekOfMonth(DateTime value)
Parameters
| Name | Type | Description |
|---|---|---|
value |
DateTime |
Returns
- int