How to get days for a date

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 days for this 31

Expected Result

 Monday (31/01/2011) 

Need help with VB6 code

Part 2

I want to find Sunday in a particular month ....

For example, if I select the month 01/2012, the query should give a result similar to this

 01 08 15 22 29 

The above dates are Sunday.

Expected Result for 01/2012 Month

 01 08 15 22 29 
+4
source share
3 answers

something like this (tested in )

final update for Sunday subquery

As requested in the comments 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 (i.e. January 31, 2011), otherwise you would have February 1, 2011 as a 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)") 
+4
source

Use a formatting function like this. Here I used Now, but you can pass any date and format of the returned string to dayname

 Format(Now, "dddd") 
0
source

You can use the DateSerial function in VB6 to convert a string variable or integer 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") 
0
source

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


All Articles