Table of Contents

Class DateTimeHandling

Namespace
ArcanaDevelopment.adTempus.Shared
Assembly
ArcanaDevelopment.adTempus.Shared.dll

Utilities for working with dates and times

public static class DateTimeHandling
Inheritance
object
DateTimeHandling

Methods

Abs(TimeSpan)

public static TimeSpan Abs(TimeSpan value)

Parameters

NameTypeDescription
value TimeSpan

Returns

TimeSpan

CalculateEaster(int)

Calculates the date of Easter for a given year.

public static DateTime CalculateEaster(int year)

Parameters

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
value1 DateTime
value2 DateTime

Returns

int

CompareTimeSpan(TimeSpan?, TimeSpan?)

Compares two nullable TimeSpan values

public static int CompareTimeSpan(TimeSpan? x, TimeSpan? y)

Parameters

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
src DateTime

The date/time to convert

srcTZBias TimeSpan

The time zone bias of the src

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

NameTypeDescription
value DateTime

Returns

Weekday

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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
date DateTime

Returns

Weekday

FormatSeconds(int)

Formats a number of seconds into a more readable format.

public static string FormatSeconds(int numberOfSeconds)

Parameters

NameTypeDescription
numberOfSeconds int

Returns

string

Remarks

FormatTimespan(TimeSpan?)

Formats a timespan into a readable format.

public static string FormatTimespan(TimeSpan? ts)

Parameters

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
value DateTime

Returns

DateTime

NextMinute(DateTimeOffset)

Gets a DateTime representing the start of the next minute

public static DateTimeOffset NextMinute(DateTimeOffset value)

Parameters

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
value DateTime?

Returns

DateTime?

The time value, or null if is null.

WeekOfMonth(DateTime)

Indicates what week of the month a value falls in.

public static int WeekOfMonth(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

int