1 Attachment(s)
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
Attachment 171633
Re: Weekday returns wrong result
Did you verify your constants?
Code:
? vbMonday
2
? vbSunday
1
Sunday is 1, to Saturday which is 7.
Re: Weekday returns wrong result
remove the vbUseSystemDayOfWeek param and see if it works (your system first day of week may have been modified)
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.
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.
1 Attachment(s)
Re: Weekday returns wrong result
@passel Here is my calendar:
Attachment 171635
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!
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)
Re: Weekday returns wrong result
Perfect explanation, if true, and I guess it is.
Thank you very much!!
Re: Weekday returns wrong result
you can use the weekdayname function like
Quote:
?weekdayname(Weekday(DateSerial(2019, 10, 1),vbUseSystemDayOfWeek),False,vbUseSystemDayOfWeek)
Tuesday
personally, i prefer a formatted string, in most cases
Quote:
?format(DateSerial(2019, 10, 1),"dddd")
Tuesday