Visual Basic Helper Routines
Pure VB: Methods to Determine Number of Days in a 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.

Routines that determine the number of days in a specified month, contributed over time to either the msnews.public.vb.* or comp.lang.vb.* newsgroups.

Note: The routines presented here are very similar, in both construction and purpose/end result, with those presented on Pure VB: Methods to Determine the Last Day of the 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
   
   For x = 0 To 1
   
      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 = "DiM - Rothstein"
      .Item(1).Caption = "DiM - Mead"
   End With
   
End Sub


Private Sub Command1_Click(Index As Integer)

   Dim result
   
   Select Case Index
      Case 0: result = DaysInMonth1(Year(Date), Month(Date))
      Case 1: result = DaysInMonth2(Date)
   End Select
   
   Label1(Index).Caption = result
   
End Sub


Private Function DaysInMonth2(ByVal d As Date) As Integer

  'Derrick Mead in microsoft.public.vb.general.discussion
   Dim dtDate As Date
   Dim dtMonth As Integer
   Dim dtYear As Integer
   
   dtMonth = Month(d)
   dtYear = Year(d)
   dtDate = DateSerial(dtYear, dtMonth, 1) 'set date to first day of Year , Month
   dtDate = DateAdd("m", 1, dtDate)        'add 1 month
   dtDate = DateAdd("d", -1, dtDate)       'subtract a day
   DaysInMonth2 = Day(dtDate)              'how many days?
   
End Function


Function DaysInMonth1(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
   DaysInMonth1 = Day(DateSerial(YearValue, _
                                 MonthValue + 1, 0))
                                   
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