Results 1 to 5 of 5

Thread: Code Snippet - Find Days In Month

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Code Snippet - Find Days In Month

    I was bored, so I coded this function to find the amount of days in a month.

    VB Code:
    1. Public Function DaysInMonth(Month As Integer, Year As Long) As Integer
    2.    
    3.     Select Case Month%
    4.        
    5.         Case Is = 1
    6.             DaysInMonth% = 31
    7.         Case Is = 2
    8.            
    9.             If (Year& Mod 4) = 0 Then
    10.                 DaysInMonth% = 29
    11.             Else:
    12.                 DaysInMonth% = 28
    13.             End If
    14.        
    15.         Case Is = 3
    16.             DaysInMonth% = 31
    17.         Case Is = 4
    18.             DaysInMonth% = 30
    19.         Case Is = 5
    20.             DaysInMonth% = 31
    21.         Case Is = 6
    22.             DaysInMonth% = 30
    23.         Case Is = 7
    24.             DaysInMonth% = 31
    25.         Case Is = 8
    26.             DaysInMonth% = 31
    27.         Case Is = 9
    28.             DaysInMonth% = 30
    29.         Case Is = 10
    30.             DaysInMonth% = 31
    31.         Case Is = 11
    32.             DaysInMonth% = 30
    33.         Case Is = 12
    34.             DaysInMonth% = 31
    35.         Case Else:
    36.             DaysInMonth% = 0
    37.        
    38.     End Select
    39.        
    40. End Function

    Enjoy,
    Sir Loin

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code Snippet - Find Days In Month

    A little less code would do the same trick
    VB Code:
    1. Public Function DaysInMonth(Month As Integer, Year As Long) As Integer
    2.    
    3.     Select Case Month
    4.         'January, March, May, July, August, October, December
    5.         Case 1, 3, 5, 7, 8, 10, 12
    6.         DaysInMonth = 31
    7.        
    8.         'Februrary
    9.         Case 2
    10.          If (Year& Mod 4) = 0 Then
    11.                 DaysInMonth = 29
    12.             Else:
    13.                 DaysInMonth = 28
    14.             End If
    15.            
    16.         'April, June, September, November
    17.         Case 4, 6, 9, 11
    18.         DaysInMonth = 30
    19.        
    20.     End Select        
    21. End Function
    22.  
    23. Private Sub Command1_Click()
    24. Dim intMonth As Integer
    25. intMonth = DaysInMonth(2, 2000)
    26. MsgBox intMonth
    27. End Sub
    Last edited by Hack; Aug 15th, 2005 at 05:40 AM.

  3. #3
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Code Snippet - Find Days In Month

    . . . and a little less code:

    VB Code:
    1. Private Function GetNumberDays(dtDate As Date) As Integer
    2. Dim strDays As String
    3.  
    4.     dtDate = Format(dtDate, "mm/yyyy")
    5.    
    6.     dtDate = DateAdd("m", 1, dtDate)
    7.     strDays = Day(dtDate - 1)
    8.    
    9.     GetNumberDays = strDays
    10. End Function

    and you would call it as follows:

    VB Code:
    1. Private Sub Command1_Click()
    2.      MsgBox GetNumberDays("2/23/2004")
    3. End Sub

    Note you must submit a vaild date for the month and year to the function.
    Last edited by Mark Gambo; Aug 17th, 2005 at 05:30 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Code Snippet - Find Days In Month

    Even less code and no variables:

    VB Code:
    1. Function GetMonthDays (MonthToCheck As Date) As Integer
    2. GetMonthDays = Day(DateAdd("d", -1, DateAdd("m", 1, DateAdd("d", -1 * (Day(MonthToCheck)-1)))))
    3. End Function

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Code Snippet - Find Days In Month

    Hardly any code and look mom "No variables":

    VB Code:
    1. Function GetMonthDays(MonthToCheck As Date) As Integer
    2.     GetMonthDays = Day((DateAdd("m", 1, Format(MonthToCheck, "mm/yyyy"))) - 1)
    3. End Function

    and call it like this:

    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox GetMonthDays("02/23/2004")
    3. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width