Search code examples
vb.netdatedayofweekweekday

vb.net - How to get the day number from a weekday name


Long story short:

I need the inverted function of WeekdayName(), in the following terms:

  1. You provide a week day name (e.g. Sunday)
  2. You get the correspondent week day number (that depends which is the first week day as returned in WeekdayName, e.g., you get 1 for Sunday if Sunday is the first day of the week)

Background:

I'm using date functions in vb.net. So far so good. I'm using the function WeekdayName() in order to set the titles for tab pages in a TabControl. My custom control (kind of calendar control) holds the mentioned TabControl and it has the option of choosing which day of the week you want to diplay.

If you want to display ALL days, the TabControl will have TabPages filled by iterating upon the WeekdayName() function mentioned above (localized and managed by the system calendar and capitalized by me).

If you set only Tuesday and Friday, you will have two tab pages with those titles and sorted as [Tuesday | Friday]. If you decide to add Wednesday, now you should have 3 tab pages: [Tuesday | Wednesday | Friday], and so on...

In order to keep tabs sorted, when I need to insert a tab to be shown I want to provide the tab's title to check against the new-to-insert weekday name and in that way have the order resolved.

I believe one workaround is to create a fake date object with the tab's title string in it in order to have a, for instance, "Sunday" date and then use: Weekday() but I was hoping an API solution.

NOTE: This is not: how to get the day number from a date.


EDIT:

As we disscused with @jmshapland, a custom code approach using the default localization and system settings could be:

Function WeekdayNumber(day as String)
    ' The following initialization does not necessary go in this function.
    ' The idea is to have a mapping between key day name and index in the
    ' same order as the WeekdayName() method with the default system settings
    Dim weekdayNamesCollection as Collection = New Collection()
    For i = 1 to WEEKDAYS
        Dim wDay as String = WeekdayName(i)
        ' add the day with a key with the same name
        weekdayNamesCollection.add(wDay, wDay)
    Next i

    If weekdayNamesCollection.ContainsKey(day) Then
        Return weekdayNamesCollection.IndexOfKey(day) + 1
    End If

    ' Raise an error as the string 'day' is not in the collection
End Sub

I didn't test this out as I am in a computer without any framework and/or IDE. But basically, the idea is to return the index where the weekday name is stored in the collection (previously, it was stored in the system settings order as WeekdayName().


Solution

  • I don't think there's a built-in function for it. But if you are using WeekdayName then you should use it for your purpose.

    Public Function WeekDayNumber(ByVal weekName As String) As Integer
    
        Dim weekNames As New Dictionary(Of String, Integer)
    
        For i As Integer = 1 To 7
            weekNames.Add(WeekdayName(i), i)
        Next
    
        Return weekNames(weekName)
    End Function