Search code examples
vb6dayofweek

VB6 week of day function


I'm relatively new to VB6 and I've just been given an assignment where I have a date - for example '4/12/2016' - from this date, i'm trying to find out the day that it is. So let's say it's a wednesday. Now from this day, I'm trying to determine the dates for the week [sun(startdate) - sat(enddate)). How would I go about doing something like this?

EDIT: I have a pretty good idea about finding out the date for sunday and saturday, since I can simply do something along the lines...

dim dateStart,dateend as date

Ex of date given to me = '4/12/2016'

Dim dateDay as variant
dateDay = whatever I get here - i'm assuming that a date will return a number for whatever day it is ???? Not sure
Select Case dateDay
case 1 -Monday?
      dateStart=dateadd("d",-1,'4/12/2016) 
      dateEnd = dateadd("d",6, '4/12/2016) 
case 2 -Tuesday?
      datestart = dateadd("d",-2,'4/12/2016)
      dateend = dateadd("d",5,'4/12/2016)

End Select

Basically do the SELECT statement for all cases. Am I on the right track?


Solution

  • This code:

    Debug.Print Format(DatePart("w", Now), "dddd")
    

    will print whatever day of the week it is now to the Immediate window. If you want the abbreviated day of week, use "ddd" for the format.

    Now, this code:

    Dim DOW As String
    Select Case DatePart("w", Now)
        Case vbSunday
            DOW = "Sunday"
        Case vbMonday
            DOW = "Monday"
        Case vbTuesday
            DOW = "Tuesday"
        Case vbWednesday
            DOW = "Wednesday"
        Case vbThursday
            DOW = "Thursday"
        Case vbFriday
            DOW = "Friday"
        Case vbSaturday
            DOW = "Saturday"
    End Select
    Debug.Print DOW
    

    will do the same thing. However, it shows you how to evaluate programmatically which day of the week you're dealing with, by using vbSunday, vbMonday, etc. That should give you what you need to get started on your Select statement. To use your example, DatePart("w", "4/12/2016") evaluates to 3, or vbTuesday.

    VB6 reference documentation is here, and rather well hidden I might add. Look up Format and DatePart to get familiar with other options.

    EDIT: As MarkL points out, the Weekday function is available in VB6 (I thought it wasn't), and is simpler (one less argument) than using DatePart. This code:

    Debug.Print Format(Weekday(Now), "dddd")
    

    will also print whatever day of the week it is to the immediate window. jac has also provided a link to the Weekday function in the comments above.