Search code examples
vb.netdatedatetimedayofweek

How to know today is which Monday, Tuesday, Wednesday, etc. (in number) of this month?


My question is:

Today's Day and Date are: Wednesday, June 8, 2016. I want to know today is which Wednesday of the current Month (like 2nd Wednesday). How I can do this using VB .NET?

I have tried the solution provided in:

How do I get first, second or last Tuesday (or any day of the week) of a given month

This solution accepts "Name of the Day" and "Week Number of the Day" and provides the output as the Date. I want the results differently as I should input the Date and the result should be the "Day Number of the Day" and "Current Day Name", like "Today is 2nd Wednesday".

Any help will be highly appreciated.


Solution

  • Try this:

        Sub Main()
            Dim checkDate As DateTime = DateTime.Now
    
            Dim infos As New Dictionary(Of Integer, String)() From {{1, "st"}, {2, "nd"}, {3, "rd"}, {4, "th"}, {5, "th"}}
            Dim dayCount As Integer = (checkDate.Day \ 7) + Convert.ToInt32((checkDate.Day Mod 7) > 0)
            Console.WriteLine("Today is the {0}{1} {2} of the month", dayCount, infos(dayCount), checkDate.DayOfWeek)
    
            Console.ReadLine()
        End Sub