dcsimg
Results 1 to 12 of 12

Thread: Weekday returns wrong result

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    126

    Question Weekday returns wrong result

    Hello!

    I have attached a screenshot. It shows my Windows calendar on the bottom right and my VB6 code on the left.

    VB6 says that the 1st of October 2019 is a vbMonday (2). But it should in fact return 3 (vbTuesday).

    What is going wrong here?

    Thank you!

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
        Dim d As Date
        d = DateSerial(2019, 10, 1)
        
        Dim iWeekday%
        iWeekday = Weekday(d, vbUseSystemDayOfWeek)
        
        MsgBox iWeekday 'returns 2 (vbMonday) while it should return 3 (vbTuesday) on my system
    
    End Sub

    Name:  weekday2.jpg
Views: 81
Size:  16.9 KB
    Last edited by tmighty2; Oct 17th, 2019 at 01:48 PM. Reason: Re-wrote my question

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,622

    Re: -

    Did you happen to run it on Tuesday?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    126

    Re: -

    No, I ran it today (a Thursday).

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,622

    Re: Weekday returns wrong result

    Did you verify your constants?

    Code:
    ? vbMonday
     2 
    
    ? vbSunday
     1
    Sunday is 1, to Saturday which is 7.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    350

    Re: Weekday returns wrong result

    remove the vbUseSystemDayOfWeek param and see if it works (your system first day of week may have been modified)

  6. #6
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,391

    Re: Weekday returns wrong result

    I think Weekday returns the day of the week as a count from the first day of the week. So any Tuesday on a system where the first day of the week is Monday will return 2. When the first day of the week is Sunday, a Tuesday would return 3.

    So in your case Monday=1, Tuesday=2, Wednesday=3, etc...It's not returning a VbDayOfWeek enumeration.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,622

    Re: Weekday returns wrong result

    Also, when I run that code, it is returning 3.

    I can't make out your calendar in your image. On my machine, October 1st, 2019 was on Tuesday, which is 3, so it appears correct. Your calendar may be different, and you should be using a different value for First day of the week?

    The constants you are referring to are for the firstdayofweek argument, not the return value. So, if you want the first day of the week to be Wednesday, then you pass vbWednesday as the second parameter, and Day of the week for a Wednesday should return 1, Thursday 2, etc...

    The Return Values description in the VB help is misleading (or do I dare say wrong), in that respect.
    Last edited by passel; Oct 17th, 2019 at 02:05 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    126

    Re: Weekday returns wrong result

    @passel Here is my calendar:
    Name:  weekday2.jpg
Views: 66
Size:  95.6 KB

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    126

    Re: Weekday returns wrong result

    @passel Thank you! I think that the problem: I didn't know what exactely Weekday returns. Now that I know what it returns, it seems to work.

    When a day is a monday, and I tell the function that the first day of the week is monday for me, then it returns 1, right?

    And yes, I actually did think that the return values was an enum. Great to know that this is not so!

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,622

    Re: Weekday returns wrong result

    Yeah, I hit F1 and the help gives you that impression, which is why I think at best it is misleading, or more likely, just wrong.
    Code:
    Return Values
    
    The Weekday function can return any of these values:
    
    Constant       Value Description 
    vbSunday       1     Sunday 
    vbMonday       2     Monday 
    vbTuesday      3     Tuesday 
    vbWednesday    4     Wednesday 
    vbThursday     5     Thursday 
    vbFriday       6     Friday 
    vbSaturday     7     Saturday
    So, while it is true, that it will only return values in the range of 1 to 7, the value returned doesn't mean what the name and Description imply, so I say the document is technically incorrect. What it returns is 1 for first day of the week, 2 for second day of the week, etc, whatever they are. (It is only true for the default case, so logically that makes it a false statement as a whole)
    Last edited by passel; Oct 17th, 2019 at 02:38 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    126

    Re: Weekday returns wrong result

    Perfect explanation, if true, and I guess it is.
    Thank you very much!!

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,626

    Re: Weekday returns wrong result

    you can use the weekdayname function like
    ?weekdayname(Weekday(DateSerial(2019, 10, 1),vbUseSystemDayOfWeek),False,vbUseSystemDayOfWeek)
    Tuesday

    personally, i prefer a formatted string, in most cases
    ?format(DateSerial(2019, 10, 1),"dddd")
    Tuesday
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width