如何获取一个月内的总天数

发布于 2024-12-24 17:20:13 字数 741 浏览 3 评论 0原文

我想按天分组查找每月的总天数。

例如

月份是 '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

上面的代码将给出结果为 Wednesday = 4, 11, 18, 25

但我想要wednesday = 4 的总数 '这样如何

在 vb6 中完成

需要 VB6 代码帮助

I want to find a total days per month group by days.

For Example

Month is '01/2011' (mm/yyyy)

Expected Output

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

Tried 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 total count of wednesday = 4 'like this

How to accomplish in vb6

Need VB6 Code Help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-12-31 17:20:13

这是一个您可以调用的函数,

它需要两个参数(年和月)并返回一个数组(1 到 7),即该月中星期日到星期六的天数

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

更新:

使用它来获取数量一个月中的星期五,使用

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

更新 2:

接受 Brettdj 的建议

要返回类似“Fridays = 4”的字符串,请使用

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

Here's a function you can call

It takes two parameters (year and month) and returns an array (1 to 7) being Sunday to Saturday number of days in the 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 number of Fridays in a month, use

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

Update 2:

Taking Brettdj 's advise

To return a string like "Fridays = 4" use

Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")(6) & " = " & Days(2012, 2)(6)
往日 2024-12-31 17:20:13

更新答案
已更新您的评论,以仅返回某

  1. 一天的一个月中的天数(在 Msgbox 提示时按 Yes),或
  2. 一周中的每一天返回一个月中的天数(按 No 在 Msgbox 提示符下)。

    Sub GetDay()  
    将 strMonth 调暗为字符串  
    将 strOut 调暗为字符串  
    暗淡日长  
    调暗检查只要  
    
    str月份 = "01/2012"
    
    lngCheck = MsgBox("按 Yes 运行单日" & vbNewLine & "按 No 运行整周", vbYesNoCancel, "用户选择")
    
    如果 lngCheck = vbCancel 则退出 Sub
    
    如果 lngCheck = vbYes 那么
    '选项 1 一天
    lngDay = vb星期五
    strOut = strOut & DaysInMonth(lngDay, strMonth) & vb换行符
    别的
    '选项 2 全天
    对于 lngDay = vbSunday 到 vbSaturday
    strOut = strOut & DaysInMonth(lngDay, strMonth) & vb换行符
    下一个
    结束如果
    
    消息框字符串输出
    结束子
    
    函数 DaysInMonth(ByVal lngDay, ByVal strMonth)
    暗淡 dtStart As 日期
    暗淡 dtEnd 作为日期
    调暗 dtTest 作为日期
    变暗只要长
    暗淡我只要
    
    dtStart = 日期值(strMonth)
    dtEnd = DateAdd("d", DateDiff("d", strMonth, DateAdd("m", 1, strMonth) - 1), dtStart)
    lngCnt = (dtEnd - dtStart + 1)
    
    DaysInMonth = WeekdayName(lngDay, , vbSunday) & “-4”
    
    对于 i = 1 至 lngCnt Mod 7
    如果工作日(DateAdd("d", i - 1, dtStart)) = lngDay 那么
     DaysInMonth = WeekdayName(lngDay, , vbSunday) & “-5”
    退出对于
    结束如果
    下一个
    
    结束功能
    

Updated Answer
Updated for your comment to return either

  1. Days in a month for one day only (Press Yes on Msgbox prompt), or
  2. Days in a month for every day in the week (Press No on Msgbox prompt).

    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
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文