C# Class EnterpriseWebLibrary.DateTimeTools

Provides helpful DateTime methods.
Datei anzeigen Open project: enduracode/enterprise-web-library

Public Methods

Method Description
DateRangesCoverAllDates ( System.DateTime beginDate, System.DateTime endDate, DateTime?>.IEnumerable dateRanges ) : bool

Returns true when each day between begin and end dates is represented inside one of the date ranges in dateRanges, inclusive. Be sure time information is not included. No begin date may be after an end date.

DateRangesOverlap ( System.DateTime rangeOneBegin, System.DateTime rangeOneEnd, System.DateTime rangeTwoBegin, System.DateTime rangeTwoEnd ) : bool

Returns true if the two given date ranges overlap. Passing null for any date means infinity in that direction. Throws an exception if any of the given dates contains time information. Use .Date if you have to. See documentation for IsBetweenDates for more information on the date ranges.

DateTimeRangeOverlapsDateRange ( System.DateTime dateTimeRangeBegin, System.DateTime dateTimeRangeEnd, System.DateTime dateRangeBegin, System.DateTime dateRangeEnd ) : bool

Returns true if the specified date/time range overlaps the specified date range. Passing null for any date means infinity in that direction. Throws an exception if the date range contains time information. Use .Date if you have to. See documentation for IsBetweenDates for more information on the date range.

DateTimeRangesOverlap ( System.DateTime rangeOneBegin, System.DateTime rangeOneEnd, System.DateTime rangeTwoBegin, System.DateTime rangeTwoEnd ) : bool

Returns true if the two given DateTime ranges overlap. Passing null for any date/time means infinity in that direction.

GetDateOfDayOfWeekInMonth ( int year, int month, DayOfWeek day, int weeksFromFirst ) : System.DateTime

Gets the date of the specified day of the week in the specified month.

GetNumberOfFullYearsBetweenDates ( System.DateTime beginDate, System.DateTime endDate ) : int

This is useful for calculating someone's age. Beginning date must be before or equal to end date.

HasTime ( this dateTime ) : bool

Returns true if this date/time contains time information.

IsBetweenDateTimes ( this dateTime, System.DateTime onOrAfterDate, System.DateTime onOrBeforeDate ) : bool

Returns true if this date is in between the given DateTimes (inclusive at beginning of range, exclusive at end of range). Passing null for either of the two dates is considered to be infinity in that direction. Therefore, passing null for both dates will always result in true.

IsBetweenDates ( this dateTime, System.DateTime onOrAfterDate, System.DateTime onOrBeforeDate ) : bool

Returns true if this date is in between (inclusive) the given dates. This method differs from IsBetweenDateTimes in that onOrAfterDate and onOrBeforeDate must be dates only (an exception will be thrown if time information is passed - use .Date if you have to.) and that it is inclusive on both ends of the range. This method also correctly returns true in the case where the dateTime parameter is 3:30PM on 11/15/09 and the onOrBeforeDate is 11/15/09. If you want to define a range with time information, use IsBetweenDateTimes instead. Passing null for either of the two dates is considered to be infinity in that direction. Therefore, passing null for both dates will always result in true.

MonthBeginDate ( this date ) : System.DateTime

Returns the begin date of the specified date's month.

ToDayMonthYearString ( this dateTime, bool useLeadingZero, bool includeDayOfWeek = false ) : string

Formats the date portion of the specified date/time in "day month year" style, e.g. 5 Apr 2008.

ToDayMonthYearString ( this dateTime, string stringIfNull, bool useLeadingZero, bool includeDayOfWeek = false ) : string

Formats the date portion of the specified date/time in "day month year" style, e.g. 5 Apr 2008. Returns stringIfNull if the specified date/time is null.

ToHourAndMinuteString ( this dateTime ) : string

Formats the time portion of the specified date/time in hour:minute style followed by a single lowercase letter indicating AM or PM.

ToHourAndMinuteString ( this dateTime, string stringIfNull ) : string

Formats the time portion of the specified date/time in hour:minute style followed by a single lowercase letter indicating AM or PM. Returns stringIfNull if the specified date/time is null.

ToMonthDayYearString ( this dateTime ) : string

Formats the date portion of the specified date/time in "01/01/2001" style.

ToMonthDayYearString ( this dateTime, string stringIfNull ) : string

Formats the date portion of the specified date/time in "01/01/2001" style. Returns stringIfNull if the specified date/time is null.

ToMonthYearString ( this dateTime ) : string

Formats the date portion of the specified date/time in "month year" style, e.g. April 2008.

WeekBeginDate ( this dateTime ) : System.DateTime

Returns the date that the given week starts on.

WeeksFromFirstOccurrenceOfDayOfWeekInMonth ( this date, bool countBackwardFromFirstOccurrenceInNextMonth ) : int

Returns the number of weeks from the first occurrence of the specified date's day of the week in the month to the specified date.

Private Methods

Method Description
assertDateTimeHasNoTime ( System.DateTime dateTime, string name ) : void

Method Details

DateRangesCoverAllDates() public static method

Returns true when each day between begin and end dates is represented inside one of the date ranges in dateRanges, inclusive. Be sure time information is not included. No begin date may be after an end date.
public static DateRangesCoverAllDates ( System.DateTime beginDate, System.DateTime endDate, DateTime?>.IEnumerable dateRanges ) : bool
beginDate System.DateTime
endDate System.DateTime
dateRanges DateTime?>.IEnumerable
return bool

DateRangesOverlap() public static method

Returns true if the two given date ranges overlap. Passing null for any date means infinity in that direction. Throws an exception if any of the given dates contains time information. Use .Date if you have to. See documentation for IsBetweenDates for more information on the date ranges.
public static DateRangesOverlap ( System.DateTime rangeOneBegin, System.DateTime rangeOneEnd, System.DateTime rangeTwoBegin, System.DateTime rangeTwoEnd ) : bool
rangeOneBegin System.DateTime
rangeOneEnd System.DateTime
rangeTwoBegin System.DateTime
rangeTwoEnd System.DateTime
return bool

DateTimeRangeOverlapsDateRange() public static method

Returns true if the specified date/time range overlaps the specified date range. Passing null for any date means infinity in that direction. Throws an exception if the date range contains time information. Use .Date if you have to. See documentation for IsBetweenDates for more information on the date range.
public static DateTimeRangeOverlapsDateRange ( System.DateTime dateTimeRangeBegin, System.DateTime dateTimeRangeEnd, System.DateTime dateRangeBegin, System.DateTime dateRangeEnd ) : bool
dateTimeRangeBegin System.DateTime
dateTimeRangeEnd System.DateTime
dateRangeBegin System.DateTime
dateRangeEnd System.DateTime
return bool

DateTimeRangesOverlap() public static method

Returns true if the two given DateTime ranges overlap. Passing null for any date/time means infinity in that direction.
public static DateTimeRangesOverlap ( System.DateTime rangeOneBegin, System.DateTime rangeOneEnd, System.DateTime rangeTwoBegin, System.DateTime rangeTwoEnd ) : bool
rangeOneBegin System.DateTime
rangeOneEnd System.DateTime
rangeTwoBegin System.DateTime
rangeTwoEnd System.DateTime
return bool

GetDateOfDayOfWeekInMonth() public static method

Gets the date of the specified day of the week in the specified month.
public static GetDateOfDayOfWeekInMonth ( int year, int month, DayOfWeek day, int weeksFromFirst ) : System.DateTime
year int The year.
month int The month.
day DayOfWeek The day of the week.
weeksFromFirst int The number of weeks from the first occurrence of the specified day of the week in the month. Pass a negative value to count /// backward from the first occurrence in the next month.
return System.DateTime

GetNumberOfFullYearsBetweenDates() public static method

This is useful for calculating someone's age. Beginning date must be before or equal to end date.
public static GetNumberOfFullYearsBetweenDates ( System.DateTime beginDate, System.DateTime endDate ) : int
beginDate System.DateTime
endDate System.DateTime
return int

HasTime() public static method

Returns true if this date/time contains time information.
public static HasTime ( this dateTime ) : bool
dateTime this
return bool

IsBetweenDateTimes() public static method

Returns true if this date is in between the given DateTimes (inclusive at beginning of range, exclusive at end of range). Passing null for either of the two dates is considered to be infinity in that direction. Therefore, passing null for both dates will always result in true.
public static IsBetweenDateTimes ( this dateTime, System.DateTime onOrAfterDate, System.DateTime onOrBeforeDate ) : bool
dateTime this
onOrAfterDate System.DateTime
onOrBeforeDate System.DateTime
return bool

IsBetweenDates() public static method

Returns true if this date is in between (inclusive) the given dates. This method differs from IsBetweenDateTimes in that onOrAfterDate and onOrBeforeDate must be dates only (an exception will be thrown if time information is passed - use .Date if you have to.) and that it is inclusive on both ends of the range. This method also correctly returns true in the case where the dateTime parameter is 3:30PM on 11/15/09 and the onOrBeforeDate is 11/15/09. If you want to define a range with time information, use IsBetweenDateTimes instead. Passing null for either of the two dates is considered to be infinity in that direction. Therefore, passing null for both dates will always result in true.
public static IsBetweenDates ( this dateTime, System.DateTime onOrAfterDate, System.DateTime onOrBeforeDate ) : bool
dateTime this
onOrAfterDate System.DateTime
onOrBeforeDate System.DateTime
return bool

MonthBeginDate() public static method

Returns the begin date of the specified date's month.
public static MonthBeginDate ( this date ) : System.DateTime
date this
return System.DateTime

ToDayMonthYearString() public static method

Formats the date portion of the specified date/time in "day month year" style, e.g. 5 Apr 2008.
public static ToDayMonthYearString ( this dateTime, bool useLeadingZero, bool includeDayOfWeek = false ) : string
dateTime this
useLeadingZero bool
includeDayOfWeek bool
return string

ToDayMonthYearString() public static method

Formats the date portion of the specified date/time in "day month year" style, e.g. 5 Apr 2008. Returns stringIfNull if the specified date/time is null.
public static ToDayMonthYearString ( this dateTime, string stringIfNull, bool useLeadingZero, bool includeDayOfWeek = false ) : string
dateTime this
stringIfNull string
useLeadingZero bool
includeDayOfWeek bool
return string

ToHourAndMinuteString() public static method

Formats the time portion of the specified date/time in hour:minute style followed by a single lowercase letter indicating AM or PM.
public static ToHourAndMinuteString ( this dateTime ) : string
dateTime this
return string

ToHourAndMinuteString() public static method

Formats the time portion of the specified date/time in hour:minute style followed by a single lowercase letter indicating AM or PM. Returns stringIfNull if the specified date/time is null.
public static ToHourAndMinuteString ( this dateTime, string stringIfNull ) : string
dateTime this
stringIfNull string
return string

ToMonthDayYearString() public static method

Formats the date portion of the specified date/time in "01/01/2001" style.
public static ToMonthDayYearString ( this dateTime ) : string
dateTime this
return string

ToMonthDayYearString() public static method

Formats the date portion of the specified date/time in "01/01/2001" style. Returns stringIfNull if the specified date/time is null.
public static ToMonthDayYearString ( this dateTime, string stringIfNull ) : string
dateTime this
stringIfNull string
return string

ToMonthYearString() public static method

Formats the date portion of the specified date/time in "month year" style, e.g. April 2008.
public static ToMonthYearString ( this dateTime ) : string
dateTime this
return string

WeekBeginDate() public static method

Returns the date that the given week starts on.
public static WeekBeginDate ( this dateTime ) : System.DateTime
dateTime this
return System.DateTime

WeeksFromFirstOccurrenceOfDayOfWeekInMonth() public static method

Returns the number of weeks from the first occurrence of the specified date's day of the week in the month to the specified date.
public static WeeksFromFirstOccurrenceOfDayOfWeekInMonth ( this date, bool countBackwardFromFirstOccurrenceInNextMonth ) : int
date this
countBackwardFromFirstOccurrenceInNextMonth bool
return int