A Dozen Date Functions and More - September 2008
Updated December 2010 and April 2011

If you download this tip please consider making a modest donation.

This month's tip combines into one class module a number of date processing functions. The functions are encapsulated in class cDates. I created an Asp.Net VB.Net Project to demonstrated the functions. Click here to run the demo.

2010/12/29 - A number of functions were added. They are described below and identified with

2011/04/22 - A lot more functions were added. They are described below and identified with

Using the Demo

Run It -  Click here to run the demo.

Calendar - Click a date on the calendar to process that date through the first group of functions. Selecting a new month causes the table to disappear - click a calendar date to make it reappear.

IsValidDates - Change either of the 'IsValidDates' then click 'Test' to test the dates. Only the first date is required. If both are entered then the second one must be greater than the first one. You could use this to test a range of dates. Dates that equal MinValue are considered invalid.

CurrentDateTimeOfDayAndBetweenHoursOf - Use this function to return True if the current day and time fall within the period specified by 'Day', 'From' and 'To'. Otherwise False is returned. Select a different 'Day', 'From' or 'To' from the drop down boxes to change their values. Click 'Test' to perform the test. Click 'Now' to reset the 'Day', 'From' and 'To' values to the current day, and time. The 'From' value is the current hour. The 'To' value is one hour later.

Class cDates Functions

  Function Operands Returns Sample
AbbreviatedDayNames
AbbreviatedMonthNames
ByVal iDay As Integer
ByVal iMonth As Integer
Integer Tue
Dec
  CurrentDateTimeOnDayAndBetweenHoursOf ByVal oDayOfWeek As DayOfWeek, _
ByVal iHourFrom As Integer, _
ByVal iHourTo As Integer
Boolean  
  LastDayOfPriorMonth
FirstDayOfCurrentMonth
LastDayOfCurrentMonth
FirstDayOfNextMonth
FirstDayOfCurrentYear
LastDayOfCurrentYear
ByVal dDate As Date DateTime  
  FormatDate ByVal dDate As Object, _
Optional ByVal bToLongDateString As Boolean = False
Optional ByVal bReturnTodayYesterdayTomorrow = False, Optional ByVal bToShortDateString = False
String 2010/12/29;
Wednesday, December 29, 2010;
Today
Fri, Apr 22, 2011
  FormatMonthYear ByVal dDate As DateTime String December, 2010
FormatDateShort ByVal dDate As DateTime, Optional bInsertCommas = True String Fri, Apr 22, 2011
FormatYYYYMMDD ByVal dDate As DateTime String 20110422
ConvertYYYYMMDD ByVal sDate As String DateTime 2011/04/22
FormatDateDDD_YYYYMMDD ByVal dDate As DateTime String Fri, 2011/04/22
FormatTime_HHmmss
FormatTime_hhmmsstt
FormatTime_hmmtt
ByVal oTime As Object, ByVal bAsNoonOrMidnight As Boolean,
Optional ByVal b24Hour As Boolean = False
String 04:20:42
04:20:42 am
4:20 am
FormatDateODBCCanonical ByRef dtSource As Date String {d '2010-12-29'}
ConvertODBCCanonicalToVBDate ByVal sODBCCanonicalDate As String DateTime  
FormatTimeStampODBCCanonical ByRef dtSource As Date String {ts '2010-12-29 04:20:42'}
  GetDaysInThisMonth
GetDaysInThisQuarter
GetDaysInThisYear
ByVal dDate As DateTime Integer 30
91
365
  IsValidDate ByVal dDate As Object Boolean  
  IsValidDates ByRef dDateValidFrom As Object, _
Optional ByRef oDateValidTo As Object = Nothing
Boolean  
IsFridaySaturdaySunday ByVal dDate As DateTime Boolean Returns true if dDate is a Friday, Saturday or Sunday.
IsLeapYear ByVal dDate As DateTime Boolean  
DaysDifference
SecondsDifference
ByVal dDate1 As DateTime, ByVal dDate2 As DateTime Integer 365
60
  DaysInThisYearsFebruary ByVal dDate As DateTime Integer 28 or 29
GetNoonOrMidnight ByVal oTime as Object String Noon or Midnight
if time is zero or .5
SundayClosestTo ByVal dDate As DateTime Date If Wednesday or less then the prior Sunday. Else next Sunday. Used in the next functions.
DayLightSavingTimeStarts ByVal dDate As DateTime Date The Sunday closest to March 14.
DayLightSavingTimeEnds ByVal dDate As DateTime Date The Sunday closest to November 7.
DayBeforeYesterdayOrEarlier ByVal dDate As DateTime Boolean Compares dDate to Today and returns True if two days earlier or more.

AbbreviatedDayNames
AbbreviatedMonthNames

Return the 3 character day or month. Current culture is considered. Source: Click here to view the source for these functions.

CurrentDateTimeOnDayAndBetweenHoursOf

This function uses the day of the week and an hour range to return True if the current datetime falls within the range. The routine is called periodically to determine if an action should be initiated during the defined period. Sample call:

Dim oDate As New cDates
If oDate.CurrentDateTimeOnDayAndBetweenHoursOf(DayOfWeek.Saturday, 1, 3) Then
   ' The Datetime is on a Saturday between 1 am and 3 am
End If

FirstDayOfCurrentMonth - FirstDayOfNextMonth
LastDayOfCurrentMonth - LastDayOfPriorMonth

Pass a date to these functions and have the appropriate date returned. Sample calls:

Dim oDate As New cDates
Dim dFirstDayOfThisMonth As Date = oDate.FirstDayOfCurrentMonth(Today)
Dim dLastDayOfThisMonth As Date = oDate.LastDayOfCurrentMonth(Today)
Dim dFirstDayOfNextMonth As Date = oDate.FirstDayOfNextMonth(Today)
Dim dLastDayOfNextMonth As Date = oDate.LastDayOfNextMonth(Today)

FormatMonthYear

Pass a date to this function and have the date returned as a month and year string. The current culture is considered. Sample call:

Dim oDate As New cDates
Dim sMonthYear As String = oDate.FormatMonthYear(Today)
 
If Today is February 22, 2009 and if the current culture is English
     then
February, 2009 is returned.

FormatDate
FormatDateShort
FormatDate - Short
FormatDate - Long
FormatDate - return Today/Yesterday/Tomorrow

Pass a date to this function and have a formatted date returned. Optionally indicate if a long date format is required or long date with abbreviated day and month. Optionally indicate if 'Today/Yesterday/Tomorrow is to be returned. A blank is returned if an invalid date or a MinValue date is passed. Sample calls:

Dim oDate As New cDates
Dim sToday As String = oDate.FormatDate(Today)
 
Returns dDate.ToShortDateString
Dim sToday As String = oDate.FormatDate(Today, True)
 
Returns dDate.ToLongDateString
Dim sToday As String = oDate.FormatDate(Today, , True)
 
Returns Today, Yesterday or Tomorrow if applicable

FormatYYYYMMDD
ConvertYYYYMMDD

Return the date as a string in yyyymmdd format. Useful for sorting. Convert a date string in format yyyymmdd and return it as a datetime.

FormatDateDDD_YYYYMMDD

Dim oDate As New cDates
Dim sToday As String = oDate.FormatDate(Today)
 
Returns a date in ddd, yyyy/mm/dd format, example: Tue, 2010/12/28.

ConvertYYYYMMDD

Convert a year, month, day string into a DateTime.

FormatTime_HHmmss
FormatTime_hhmmsstt
FormatTime_hmmtt

Returns the time in various formats. See the sample above. Pass the time in a variable, for example: .FormatTime_HHmmss(Now, True) The second operand, if True, returns 'Noon' or 'Midnight' if applicable.

FormatDateODBCCanonical
FormatTimeStampODBCCanonical
ConvertODBCCanonicalToVBDate

These functions are useful when creating SQL queries. Provide a date in ODBC Canonical format when you want to select records which contain a specified date. I have not found a use for the Time Stamp format. Use ConvertODBCCanonicalToVBDate to convert the string back to a Date.

DaysDifference - SecondsDifference

Provide two dates or times to these functions. The number of days or seconds difference is returned as an integer.

GetDaysInThisMonth - GetDaysInThisQuarter - GetDaysInThisYear

Pass a date to these functions and have the number of days in the period represented by the date returned. Sample calls:

Dim oDate As New cDates
Dim iDaysInThisMonth As Integer = oDate.GetDaysInThisMonth(Today)
Dim iDaysInThisQuarter As Integer = oDate.GetDaysInThisQuarter(Today)
Dim iDaysInThisYear As Integer = oDate.GetDaysInThisYear(Today)

IsValidDate - IsValidDates

Pass a date to IsValidDate or pass a starting and finishing date to IsValidDates. True is returned if the dates pass a series of tests.

Dim oDate As New cDates
If oDate.IsValidDate(dDate) Then
  ' dDate is Valid
End If
If oDate.IsValidDates(dDateFirst, dDateLast) Then
  ' Both dates are valid and dDateLast is greater than dDateFirst.
End If

IsFridaySaturdaySunday
IsLeapYear
IsDayBeforeYesterdayOrEarlier

Provide a date. True is returned if the condition is meet.

DaysInThisYearsFebruary - LeapDaysForThisFebruary

Pass a date to this function. The number of days in this year's February is returned. Either 28 or 29.

2009/02/22 - Added alias DaysInThisYearsFebruary. It is more descriptive.

Dim oDate As New cDates
Dim iFebruaryDaysThisYear As Integer = oDate.DaysInThisYearsFebruary(Today)

Download VB.Net Code

Download - VS2005 or VS2008 VB.Net Zipped File  Size 
DateFunctions Project with a Solution File - VS/2005
Does not contain the functions.
Not recommended but still provided.
DateFunctionsSLN.zip 31k
DateFunctions Project with a Solution File - VS/2008 DateFunctionsSLN20110422.zip 39K

 Click here to run the demo.

Previous Tip   Tip Index   Next Tip

Home

Updated 2011/04/23