当前位置 : 主页 > 编程语言 > c语言 >

如何获得一个月的总日数

来源:互联网 收集:自由互联 发布时间:2021-06-24
我希望按天计算每月总天数. 例如 月是’01 / 2011′(mm / yyyy) Expected OutputSunday - 5Monday - 5Tuesday - 5Wednesday - 4Thursday - 4Friday - 4Saturday - 4 试过的代码 Dim lngCnt As Long Dim strOut As String dtStart = Da
我希望按天计算每月总天数.

例如

月是’01 / 2011′(mm / yyyy)

Expected Output

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

试过的代码

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

上述代码将给出结果为星期三= 4,11,18,25

但我希望像这样的周三= 4’的总数

如何在vb6中完成

需要VB6代码帮助

更新的答案
更新了您的评论以返回

>仅限一天的一个月中的天数(在Msgbox提示符下按是),或
>一周中每天的一个月天数(在Msgbox提示下按否).

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
网友评论