How to get the total number of days per month

I want to find the total number of days per month by day.

Example

Month '01/2011' (mm / yyyy)

 Expected Output Sunday - 5 Monday - 5 Tuesday - 5 Wednesday - 4 Thursday - 4 Friday - 4 Saturday - 4 

Trial code

 Dim lngCnt As Long Dim strOut As String dtStart = DateValue('01/2012') dtEnd = DateAdd("d", DateDiff("d", '01/2012', DateAdd("m", 1, '01/2012') - 1), dtStart) lngCnt = Weekday(dtStart) - 3 Do lngCnt = lngCnt + 3 strOut = strOut & Format(lngCnt, "00") & "," Loop While lngCnt + 3 <= dtEnd - dtStart 

The above code will give the result as Wednesday = 4, 11, 18, 25

But I want the total wednesday = 4 'like this

How to run in vb6

Need help with VB6 code

+4
source share
2 answers

Updated answer Updated for your comment to return either

  • Days per month for just one day (click Yes on the Msgbox command line) or
  • Days per month for each day of the week (click No on the Msgbox command line).

     Sub GetDay() Dim strMonth As String Dim strOut As String Dim lngDay As Long Dim lngCheck As Long strMonth = "01/2012" lngCheck = MsgBox("Press Yes to run single day" & vbNewLine & "Press No to run the entire week", vbYesNoCancel, "User choice") If lngCheck = vbCancel Then Exit Sub If lngCheck = vbYes Then 'Option 1 one day lngDay = vbFriday strOut = strOut & DaysInMonth(lngDay, strMonth) & vbNewLine Else 'Option 2 all days For lngDay = vbSunday To vbSaturday strOut = strOut & DaysInMonth(lngDay, strMonth) & vbNewLine Next End If MsgBox strOut End Sub Function DaysInMonth(ByVal lngDay, ByVal strMonth) Dim dtStart As Date Dim dtEnd As Date Dim dtTest As Date Dim lngCnt As Long Dim i As Long dtStart = DateValue(strMonth) dtEnd = DateAdd("d", DateDiff("d", strMonth, DateAdd("m", 1, strMonth) - 1), dtStart) lngCnt = (dtEnd - dtStart + 1) DaysInMonth = WeekdayName(lngDay, , vbSunday) & " - 4" For i = 1 To lngCnt Mod 7 If Weekday(DateAdd("d", i - 1, dtStart)) = lngDay Then DaysInMonth = WeekdayName(lngDay, , vbSunday) & " - 5" Exit For End If Next End Function 
+2
source

Here you can call the function

It takes two parameters (year and month) and returns an array (1 to 7) from Sunday to Saturday the number of days in a month

 Function Days(yr As Long, mn As Long) As Variant Dim First As Date Dim FirstDay As Long Dim DaysInMonth As Long Dim DayCount(1 To 7) As Long Dim i As Long DayCount(1) = 4 DayCount(2) = 4 DayCount(3) = 4 DayCount(4) = 4 DayCount(5) = 4 DayCount(6) = 4 DayCount(7) = 4 First = DateSerial(yr, mn, 1) DaysInMonth = DateSerial(yr, mn + 1, 1) - First FirstDay = Weekday(First) For i = FirstDay To DaysInMonth + FirstDay - 28 - 1 DayCount((i - 1) Mod 7 + 1) = 5 Next Days = DayCount End Function 

UPDATE:

to use this to get the number of fridays per month use

 Fridays = Days(2012, 2)(6) ' For Fridays in Fedruary 2012 

Update 2:

Taking Brettd's recommendations

To return a string, for example "Fridays = 4", use

 Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")(6) & " = " & Days(2012, 2)(6) 
+3
source

Source: https://habr.com/ru/post/1389060/


All Articles