Visual Basic Helper Routines
Pure VB: Methods to Determine the Last Day of the Month
     
Posted:   Monday March 25, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Newsgroup contributors
     

Related:  

Pure VB: Methods to Determine the Last Day of the Month
Pure VB: Methods to Determine Number of Days in a Month
Pure VB: Determining Leap Years
VarMonthName: Retrieve Localized Month Names
VarWeekdayName: Retrieve Localized Weekday Names
     
 Prerequisites
None.

A variety of routines to determine whether a specified date is the last day of the current month, or, given a month and year, the last day of that month. All have been contributed over time to either the msnews.public.vb.* or comp.lang.vb.* newsgroups.  (LDiM = LastDayInMonth, LDoM=LastDayOfMonth). Use the method you find most appropriate.

Note: The routines presented here are very similar, in both construction and purpose/end result, with those presented on Pure VB: Methods to Determine Number of Days in a Month. Therefore examination of the routines on that page may provide additional information or insight.

 

 BAS Module Code
None.

 Form Code
Toss a command button and label onto a form, and set the Index property of each to 0 to create control arrays.  And add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()
   
   Dim x As Long
   
  'load and position required controls
   For x = 0 To 4
   
      If x > 0 Then
         Load Command1(x)
         Load Label1(x)
      End If
      
      Command1(x).Move 300, 500 + (400 * (x + 1)), 2535
      Command1(x).Visible = True
      
      Label1(x).Move Command1(x).Width + _
                     Command1(x).Left + 400, _
                     430 * (x + 1), 2535
      
      Label1(x).Move Command1(x).Width + _
                     Command1(x).Left + 400, _
                     Command1(x).Top + _
                     Command1(x).Height - _
                     Label1(x).Height
                     
      Label1(x).Visible = True
      Label1(x).Caption = "Ready"
      
   Next
   
   With Command1
      .Item(0).Caption = "LDiM 1 - Rothstein"
      .Item(1).Caption = "LDoM 1 - Copeland 1"
      .Item(2).Caption = "LDoM 1 - Copeland 2"
      .Item(3).Caption = "LDoM 1 - Jacquemin"
      .Item(4).Caption = "LDoM 1 - Kenny (MS)"
   End With
   
End Sub


Private Sub Command1_Click(Index As Integer)

   Dim result
   Dim sDate As Date
   
   sDate = Format$("02/28/2002", "short date")
   
   Select Case Index
      Case 0: result = LastDayInMonth(Year(Date), Month(Date))
      Case 1: result = LastDayOfMonth1(sDate)
      Case 2: result = LastDayOfMonth2(sDate)
      Case 3: result = LastDayOfMonth3(sDate)
      Case 4: result = LastDayOfMonth4(sDate)
   End Select
   
   Label1(Index).Caption = result
   
End Sub


Function LastDayInMonth(YearValue As Long, _
                        MonthValue As Long) As Long
                       
  'Rick Rothstein in microsoft.public.vb.general.discussion
  
  'For the specified year and month,
  'return the last day of that month
   LastDayInMonth = Day(DateSerial(YearValue, _
                                   MonthValue + 1, 0))
                                   
End Function


Private Function LastDayOfMonth1(ByVal d As Date) As Boolean

  'George Copeland in comp.lang.basic.visual
   LastDayOfMonth1 = CBool(DatePart("m", d) - _
                           DatePart("m", (DateAdd("d", 1, d))))
                       
End Function


Private Function LastDayOfMonth2(ByVal d As Date) As Boolean

  'George Copeland in comp.lang.basic.visual
   LastDayOfMonth2 = CBool(Month(d) - Month(DateAdd("d", 1, d)))

End Function


Private Function LastDayOfMonth3(ByVal d As Date) As Boolean

  'Keith Jacquemin in comp.lang.basic.visual
   If Month(d) <> Month(d + 1) Then
      LastDayOfMonth3 = True
   Else
      LastDayOfMonth3 = False
   End If
    
End Function


Private Function LastDayOfMonth4(ByVal srcDate As Date) As Date

  'Kenny (ms support) in microsoft.public.vb.general.discussion
  
  'passed a source date, returns a complete
  'date for the last day of the month
   Dim d As Date
   d = DateAdd("m", 1, srcDate)
   d = DateAdd("d", -Day(d), d)
   LastDayOfMonth4 = d
    
End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter