How to get the days for the date

会有一股神秘感。 提交于 2019-12-30 10:03:16

问题


Part 1

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

The above code will return "31"

I want to get the days for that 31

Expected Output

Monday (31/01/2011)

Need VB6 Code Help

Part 2

I want to find the sundays on the particular month....

For Example, If i select the month 01/2012, The query should give the result like this

01
08
15
22
29

The above dates are sunday.

Expected Output for 01/2012 Month

01
08
15
22
29

回答1:


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), "dddd (dd/mm/yyyy)")

old

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



回答2:


Use format function like this. Here i used Now but you can pass any date and format return string of dayname

Format(Now, "dddd")



回答3:


You can use DateSerial Function in VB6 to convert a string or integer variable to Date Variable

Dim d As String
Dim datevar As Date
d = "31"
datevar = DateSerial(2011,1, Val(d))   
MsgBox Format(datevar,"DDDD dd/MMM/yyyy")


来源:https://stackoverflow.com/questions/8698963/how-to-get-the-days-for-the-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!