How to get the days for the date

后端 未结 3 543
天涯浪人
天涯浪人 2021-01-13 21:50

Part 1

Dim totdays as long
totdays = DateDiff(\"d\", \"01/2011\", DateAdd(\"m\", 1, \"01/2011\"))

The above code will retu

3条回答
  •  星月不相逢
    2021-01-13 21:58

    something like this (tested in vba)

    final update for the Sunday sub-query

    As per request in commentd from Gopal below

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

    updated

    Note that I needed to use lngdays-1 to add 1 day less than a month (ie 31-Jan-2011), else you would have had 01-Feb-2011 as the result

    Dim strDate As String
    Dim lngdays As Long
    strDate = "01/2011"
    lngdays = DateDiff("d", strDate, DateAdd("m", 1, strDate))
    MsgBox Format(DateAdd("d", lngdays - 1, strDate), "ffffdd (dd/mm/yyyy)")
    

    old

     Dim lngdays As Long
     lngdays = DateDiff("d", "01/2011", DateAdd("m", 1, "01/2011"))
     MsgBox Format(DateSerial(2011, 1, lngdays), "ffffdd (dd/mm/yyyy)")
    

提交回复
热议问题