VB Code:
  1. ' Age              Age in years.
  2. ' DaysInMonth      The number of days in the current month.
  3. ' DaysInMonth2     Alternate method.
  4. ' EndOfMonth       Returns the date for the last day of the current month.
  5. ' EndOfWeek        Returns the date for the last day in the current week.
  6. ' LastBusDay       Returns the date for the last business day (Mon-Fri)
  7. '                 in the current month.
  8. ' LeapYear         Returns True or False if the year is a leap year.
  9. ' LeapYear2        Alternate method.
  10. ' NextDay          Returns the date for the next day (Sun...Sat) after the
  11. '                 current date.
  12. ' NextDay1         Returns the date for the next day (Sun...Sat) on or
  13. '                 after the current date.
  14. ' PriorDay         Returns the date for the last day (Sun...Sat) before
  15. '                 the current date.
  16. ' PriorDay1        Returns the date for the last day (Sun...Sat) on or
  17. '                 before the current date.
  18. ' StartOfMonth     Returns the date for the first day of the current
  19. '                 month.
  20. ' StartOfWeek      Returns the date for the first day of the current week.
  21.  
  22.  
  23. Function Age(ByVal Bdate As Date, ByVal DateToday As Date) As Long
  24. ' Doesn't handle negative date ranges i.e. Bdate > DateToday.
  25.   If Month(DateToday) < Month(Bdate) _
  26.   Or (Month(DateToday) = Month(Bdate) _
  27.   And Day(DateToday) < Day(Bdate)) Then
  28.     Age = Year(DateToday) - Year(Bdate) - 1
  29.   Else
  30.     Age = Year(DateToday) - Year(Bdate)
  31.   End If
  32. End Function
  33.  
  34. Function DaysInMonth(ByVal D As Date) As Long
  35. ' Requires a date argument because February can change
  36. ' if it's a leap year.
  37.   Select Case Month(D)
  38.     Case 2
  39.       If LeapYear(Year(D)) Then
  40.         DaysInMonth = 29
  41.       Else
  42.         DaysInMonth = 28
  43.       End If
  44.     Case 4, 6, 9, 11
  45.       DaysInMonth = 30
  46.     Case 1, 3, 5, 7, 8, 10, 12
  47.       DaysInMonth = 31
  48.   End Select
  49. End Function
  50.  
  51. Function DaysInMonth2(ByVal D As Date) As Long
  52. ' Requires a date argument because February can change
  53. ' if it's a leap year.
  54.   DaysInMonth2 = Day(DateSerial(Year(D), Month(D) + 1, 0))
  55. End Function
  56.  
  57. Function EndOfMonth(ByVal D As Date) As Date
  58.   EndOfMonth = DateSerial(Year(D), Month(D) + 1, 0)
  59. End Function
  60.  
  61. Function EndOfWeek(ByVal D As Date) As Date
  62.   EndOfWeek = D - Weekday(D) + 7
  63. End Function
  64.  
  65. Function LastBusDay(ByVal D As Date) As Date
  66. Dim D2 As Variant
  67.   D2 = DateSerial(Year(D), Month(D) + 1, 0)
  68.   Do While Weekday(D2) = 1 Or Weekday(D2) = 7
  69.     D2 = D2 - 1
  70.   Loop
  71.   LastBusDay = D2
  72. End Function
  73.  
  74. Function LeapYear(ByVal YYYY As Long) As Boolean
  75.   LeapYear = YYYY Mod 4 = 0 _
  76.              And (YYYY Mod 100 <> 0 Or YYYY Mod 400 = 0)
  77. End Function
  78.  
  79. Function LeapYear2(ByVal YYYY As Long) As Boolean
  80.   LeapYear2 = Month(DateSerial(YYYY, 2, 29)) = 2
  81. End Function
  82.  
  83. Function NextDay(ByVal D As Date, ByVal DayCode As Long) As Date
  84. ' DayCode (1=Sun ... 7=Sat) or use vbSunday...vbSaturday.
  85.   NextDay = D - Weekday(D) + DayCode + _
  86.             IIf(Weekday(D) < DayCode, 0, 7)
  87. End Function
  88.  
  89. Function NextDay1(ByVal D As Date, ByVal DayCode As Long) As Date
  90.   NextDay1 = D - Weekday(D) + DayCode + _
  91.              IIf(Weekday(D) <= DayCode, 0, 7)
  92. End Function
  93.  
  94. Function PriorDay(ByVal D As Date, ByVal DayCode As Long) As Date
  95.   PriorDay = D - Weekday(D) + DayCode - _
  96.              IIf(Weekday(D) > DayCode, 0, 7)
  97. End Function
  98.  
  99. Function PriorDay1(ByVal D As Date, ByVal DayCode As Long) As Date
  100.   PriorDay1 = D - Weekday(D) + DayCode - _
  101.               IIf(Weekday(D) >= DayCode, 0, 7)
  102. End Function
  103.  
  104. Function StartOfMonth(ByVal D As Date) As Date
  105.   StartOfMonth = DateSerial(Year(D), Month(D), 1)
  106. End Function
  107.  
  108. Function StartOfWeek(ByVal D As Date) As Date
  109.   StartOfWeek = D - Weekday(D) + 1
  110. End Function